繁体   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