Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:52:48+00:00 2026-06-14T02:52:48+00:00

My file program works fine. It creates a txt file with the contents I

  • 0

My file program works fine. It creates a txt file with the contents I submitted. But, why does my console display ‘true’ infinitely?

enter image description here

        Scanner input = new Scanner(System.in);
        System.out.println("What would you like the file to be called?");
        String output = input.nextLine();

        if (!output.contains(".txt")) {
            output += ".txt";
        }

        PrintWriter out = new PrintWriter(output);
        System.out.println("What would you like to print to the file? ");
        String fileContents = input.nextLine();
        out.print(fileContents);
        out.close();

        Scanner fromFile = new Scanner(new File(output));

        System.out.println("The File " + output + " contains: ");

        while (fromFile.hasNextLine()) {
            System.out.println(fromFile.hasNextLine());
        }

        System.out.println("Finished reading and writing from file.. ");
        input.close();
        fromFile.close();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:52:50+00:00Added an answer on June 14, 2026 at 2:52 am

    You are not actually retrieving the next line

    while (fromFile.hasNextLine()) {
        System.out.println(fromFile.hasNextLine());
    }
    

    Your print statement is simply re-checking that it has a newline. I think what you meant to do was

    System.out.println(fromFile.nextLine());
    

    By calling nextLine() you are telling Scanner that you want to consume all the characters up to the next '\n' (newline) character. See the Scanner class for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I compile and run my program in Eclipse and everything works fine, but when
My program file is encoded in UTF-8 so abc.length == 3 but åäö.length ==
I have a file transfer program. The program (Client) does following operations to send
CruiseControl.net creates (by default) for each project a subdirectory under: c:\Program File\CruiseControl.NET\server How can
I whipped up a quick program to simply grab the contents of a file
I'm trying to create a program that reads in a .txt file with multiple
My python program (Python 2.6) works fine when I run it using the Python
I have made a SSIS package to create an XML file, which works fine
open() fails with ENOENT (no such file or directory) on first attempt but works
Iam trying to redirect Console.Out to two text files using Console.SetOut . Console.SetOut(File.CreateText(c:\\del1.txt)); Console.WriteLine(string1);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.