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

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();
}
You are not actually retrieving the next line
Your print statement is simply re-checking that it has a newline. I think what you meant to do was
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.