java - 扫描程序不会从文件中读取下一行。 java

原文 标签 java output bufferedreader

就像标题所说的那样,我可以将第一件事写到我想要的文件中,但是之后就不再写了。我通过调试器运行它,发现它甚至没有读取下一行(我知道这一点,因为它没有填充数组。)我尝试手动推进指针(我不知道这是否真的可以做)在循环的末尾添加了“ 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();
 }

关于java - 扫描程序不会从文件中读取下一行。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074778/

相关文章:

java - 抽象类使用它自己的抽象方法?

java - 如何将5行数据存储为ArrayList中的1个元素?

c++ - 直方图程序给出奇怪的输出C++

PostgreSQL 导出 COMPOUND 查询输出到 CSV

java - 传递给另一个函数时,BufferedReader可以继续读取文本文件中的行吗?

java - 如何读取我在同一包内创建的文件?

java - 如何在JAVA中使用API​​创建新用户

java - 在模块 10 中生成金字塔以显示数字

java - 是fork/join多线程吗?

c - C面试输出