您好我有這樣的Java代碼,如何在Java中使用Scanner類及其next()方法?
import java.io.*;
import java.util.*;
public class SongWriter
{
public static void main(String[] args)
{
PrintWriter outputStream = null; // Scope must be outside the try/catch structure
try
{
outputStream = new PrintWriter("Song.txt"); // new
FileOutputStream("Song.txt")
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file Song.txt.");
System.exit(0);
}
System.out.println("\n classical songs has many lines");
System.out.println("\nNow enter the three lines of your Song.");
String line = null;
Scanner keyboard = new Scanner(System.in);
int count;
for (count = 1; count <= 3; count++)
{
System.out.println("\nEnter line " + count + ": ");
line = keyboard.nextLine();
outputStream.println(count + "\t" + line);
}
outputStream.close();
System.out.println("\nYour Song has been written to the file Song.txt.\n");
} // end of main
} // end of class
如何調整的程序,所以它首先要求寫入文件的名稱。使用Scanner類和它的next()方法。在通知讀者文件名應以後綴.txt 告終後,請在文件名中作爲字符串變量讀取。例如: - 文件名爲Haiku1.txt,Haiku2.txt和Haiku3.txt的歌曲。
如果您添加空格以提高可讀性,這將非常有幫助 – Deena
您是否閱讀過Scanner類的Javadocs? –
@Deena對不起,我修好了。 – Sameena