簡體   English   中英

掃描程序不會從文件中讀取下一行。 JAVA

[英]Scanner won't read the next line from a file. JAVA

就像標題所說的那樣,我可以將第一件事寫到我想要的文件中,但是之后就不再寫了。 我通過調試器運行它,發現它甚至沒有讀取下一行(我知道這一點,因為它沒有填充數組。)我嘗試手動推進指針(我不知道那是否實際上是您可以做的事情)在循環的末尾添加了“ line = in.readline;”,但它只是拋出了“ nosuchelement”異常。 這是我的嘗試塊”

try
  {
     in = new BufferedReader(new FileReader("lab7input.txt"));

     outNormal = new PrintWriter(new File("normal.txt"));
     outVegetarian = new PrintWriter(new File("vegetarian.txt"));
     outPescetarian = new PrintWriter(new File("pescetarian.txt"));
     outInvalid = new PrintWriter(new File("invalid.txt"));

     String line = in.readLine();
     StringTokenizer st = new StringTokenizer(line);

     while (line != null)
     {
        Scanner sc = new Scanner(line);
        while(sc.hasNext())
        {
            if(st.countTokens() == 3)
            {
                attendee3[0] = sc.next();
                attendee3[1] = sc.next();
                attendee3[2] = sc.next();

                if(Integer.parseInt(attendee3[2]) == 0)
                {
                    outNormal.println(attendee3[0] + " " + attendee3[1]);
                    outNormal.close();
                }
                else if(Integer.parseInt(attendee3[2]) == 1)
                {
                    outVegetarian.println(attendee3[0] + " " + attendee3[1]);
                    outVegetarian.close();
                }
                else if(Integer.parseInt(attendee3[2]) == 2)
                {
                    outPescetarian.println(attendee3[0] + " " + attendee3[1]);
                    outPescetarian.close();
                }
                else
                {
                    outInvalid.println(attendee3[0] + " " + attendee3[1]);
                    outInvalid.close();
                }
            }  
            if(st.countTokens() == 4)
            {
                attendee4[0] = sc.next();
                attendee4[1] = sc.next();
                attendee4[2] = sc.next();

                if(Integer.parseInt(attendee4[3]) == 0)
                {
                    outNormal.println(attendee4[0] + " " + attendee4[1] + " " + attendee4[2]);
                    outNormal.close();
                }
                else if(Integer.parseInt(attendee4[3]) == 1)
                {
                    outVegetarian.println(attendee4[0] + " " + attendee4[1] + " " + attendee4[2]);
                    outVegetarian.close();
                }
                else if(Integer.parseInt(attendee4[3]) == 2)
                {
                    outPescetarian.println(attendee4[0] + " " + attendee4[1] + " " + attendee4[2]);
                    outPescetarian.close();
                }
                else
                {
                    outInvalid.println(attendee4[0] + " " + attendee4[1] + " " + attendee4[2]);
                    outInvalid.close();
                }
            }
            //line = in.readLine();
        }
     }
  }

一個更簡單,更清潔的方法是

 String line = in.readLine();

 while (line != null)
 {
    String arr [] = line.split ();
    if(arr.length == 3)
    {
            attendee3[0] = arr[0];
            attendee3[1] = arr[1];
            attendee3[2] = arr[2];
    }
   // other count code

   line = in.readLine();
 }

您必須將line = in.readLine()放在下面的一個塊中,而不是放在掃描儀的while塊中。

不僅如此,我還具有Scanner只能讀取UTF-8編碼的.txt文件的經驗。 因此,請務必確保它們均采用UTF-8編碼。
如果不是,只需打開文件並使用該代碼保存即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM