public class FileReaderProgram
{
String stringComponent;
int integerComponent;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the absolute path of the file");
String fileName = in.next(); //Gets the file from the user
File inFile = new File(fileName);
Scanner fileReader = new Scanner(inFile); //Constructs Scanner for reading the file
while (fileReader.hasNextLine())
{
String line = fileReader.nextLine(); //Gets line from the file
Scanner lineScanner = new Scanner(line); //Constructs new scanner to analize the line
String stringComponent = lineScanner.next(); //Obtains the first word of the data line
while (!lineScanner.hasNextInt()) // Checks if there is another word in the string portion of the line
{
stringComponent = stringComponent + " " + lineScanner.next();
}
int integerComponent = lineScanner.nextInt(); //Obtains the integer part of the data line
lineScanner.nextLine(); //Consume the newline
}
System.out.println(stringComponent);
System.out.println("integerComponent);
}
}
我的簡單的小那的應該讀取的文件的多行(包含字符串和一個整數的每一行由一個分離的碼逗號),然後讀回給我,所以我知道它的工作。但是,我在兩個變量的標題中都收到了錯誤消息,並在System.out中突出顯示了它們的位置。我完全困惑,爲什麼,請幫忙?「非靜態變量不能從靜態上下文引用的」我只有1類
刪除開頭的前兩個語句(成員變量)。 – Rao