我在使用OOP在Java中讀取數據的第一行時遇到了一些問題。這個工作沒有OOP這裏:OOP文件讀取問題
Scanner in = new Scanner(System.in); // sets up scanner
System.out.print("Enter file name: "); //user provides file name and location
String userFile = in.nextLine(); // accepts input from user
File file = new File(userFile); //uses the file method to import the data
Scanner inputFile = new Scanner(file); // uses scanner to read the data
String fileContents = inputFile.nextLine();
System.out.print(fileContents);
但是我不能」得到這個在類文件的工作,因爲該方法nextLine()是未定義String類型,即使我真的只是使用上面。
public String Out(String userIn)
{
String nOfStudentsIndex = userIn.nextLine();
此外,爲什麼我不能以這種方式傳遞文件到其他類文件?
Scanner in = new Scanner(System.in); // sets up scanner
System.out.print("Enter file name: "); //user provides file name and location
String userFile = in.nextLine(); // accepts input from user
File file = new File(userFile); //uses the file method to import the data
Scanner inputFile = new Scanner(file); // uses scanner to read the data
System.out.println(inputFile.Out());
'String'沒有'nextLine()','Scanner'一樣。 – GiantTree