1
我的文件程序工作正常。它用我提交的內容創建一個txt文件。但是,爲什麼我的控制檯無限顯示「真實」?如何解決檢查掃描儀是否有另一行輸入的循環?
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();
}