【问题标题】:File Not Read Properly文件未正确读取
【发布时间】:2021-01-05 12:15:43
【问题描述】:

我在读取文件时遇到问题。我从文件中获取数据,但是当我去显示它们时,输出看起来越来越不对了。这是我的代码。我不想要一行数字。我只希望它们像处理第一行数字一样直接下来。我认为printf 会有所帮助,但它没有。

File f = new File("divers.dat");
Scanner kb = new Scanner(f);

while (kb.hasNext()) {
            
            String name = kb.nextLine();
            String school = kb.nextLine();
            String s1 = kb.next();
            String s2 = kb.next();
            String s3 = kb.next();
            String s4 = kb.next();
            String s5 = kb.next();
            String s6 = kb.next();
            String s7 = kb.next();
            String diff = kb.next();
            
            System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s", name, school, s1, s2, s3, s4, s5, s6, s7, diff);
}

这是我的文件格式:

Lucieta Spinelli
Bryn Mawr College
8 9 8 7 8 8 8 2.5
7 8 7 6 7 7 6 3.7
8 7 6 7 7 6 8 3.2
LiPing Sun
Rutgers University
6.5 5 9.5 7.5 8 8.5 7 2.3
7 9 7.5 9 8.5 6.5 8 2.6
6 4 5 9 7 8 6 3.0
Greg McEntire
University of Southern California
9 8 8.5 9.5 9 9.5 7 2.3
9 7 8 8 7.5 8.5 7.5 2.6
8 8 8 8 8 8 8 3.4
Shiva Vadaparthy
Northwestern University
9 8 9.5 9.5 7.5 8 8.5 3.0
8 9 8.5 7.5 9 8.5 8 3.6
7.5 9 8 7 8.5 8 7 4.1
Elizabeth Bennet
Oxford University
7 8 9 8 7 7 8 2.4
8 8 6 7 9 7 8 2.7
8 7 8 6 9 8 9 3.0
Sofia Berrios
Universidad Simon Bolivar
8.5 7.5 9.5 9 7 8.5 8 2.4
8 7 8 9 8 6 7 3.0
7 5.5 6.5 7 5 6.5 7 3.6
Amir Majid
Reed College
4 5 6 5 3 5 7 2.8
7 6 7 8 7 9 8 2.9
8 9 7 9 8.5 8 9 3.4
James Pajuio
University of Pennsylvania
8 7 8 9 8 6 7 2.8
7.5 9 8 7 8.5 8 7 3.0
7.5 9 8 7 8.5 8 7 4.3
Sharon Lewis
Howard University
8 9 9.5 9 8.5 8 9 2.7
4 5 5.5 7 6.5 4.5 5 3.2
8 7 8 9 8 8 7 4.0
Divya Patel
Temple University
7 9 7.5 9 8.5 6.5 8 2.1
8 7 8 6 9 8 9 2.3
3.5 5.5 4 5 3 4 4.5 4.3
Guo Zhang
Portland State University
5 6 5.5 7 6.5 7 5.5 6 3.0
8 7 8.5 9 7.5 8.5 8 3.2
9 8 9.5 8.5 9 8.5 9.5 4.1
Tomasz Wojaczyk
Tufts University
8 8 7 8 7 8 7 2.6
5 4 5 6 7 5 5 2.9
8 6 6 7 8 7 6 3.6

这是输出:

Lucieta Spinelli
Bryn Mawr College
8
9
8
7
8
8
8
2.5
7 8 7 6 7 7 6 3.7
8
7
6
7
7
6
8
3.2
LiPing Sun
Rutgers
University
6.5
5
9.5
7.5
8
8.5 7 2.3
7 9 7.5 9 8.5 6.5 8 2.6
6
4
5
9
7
8
6
3.0
Greg McEntire
University
of
Southern
California
9
8
8.5
9.5 9 9.5 7 2.3
9 7 8 8 7.5 8.5 7.5 2.6
8
8
8
8
8
8
8
3.4
Shiva Vadaparthy
Northwestern
University
9
8
9.5
9.5
7.5
8 8.5 3.0
8 9 8.5 7.5 9 8.5 8 3.6
7.5
9
8
7
8.5
8
7
4.1
Elizabeth Bennet
Oxford
University
7
8
9
8
7
7 8 2.4
8 8 6 7 9 7 8 2.7
8
7
8
6
9
8
9
3.0
Sofia Berrios
Universidad
Simon
Bolivar
8.5
7.5
9.5
9
7 8.5 8 2.4
8 7 8 9 8 6 7 3.0
7
5.5
6.5
7
5
6.5
7
3.6
Amir Majid
Reed
College
4
5
6
5
3
5 7 2.8
7 6 7 8 7 9 8 2.9
8
9
7
9
8.5
8
9
3.4
James Pajuio
University
of
Pennsylvania
8
7
8
9
8 6 7 2.8
7.5 9 8 7 8.5 8 7 3.0
7.5
9
8
7
8.5
8
7
4.3
Sharon Lewis
Howard
University
8
9
9.5
9
8.5
8 9 2.7
4 5 5.5 7 6.5 4.5 5 3.2
8
7
8
9
8
8
7
4.0
Divya Patel
Temple
University
7
9
7.5
9
8.5
6.5 8 2.1
8 7 8 6 9 8 9 2.3
3.5
5.5
4
5
3
4
4.5
4.3
Guo Zhang
Portland
State
University
5
6
5.5
7
6.5 7 5.5 6 3.0
8 7 8.5 9 7.5 8.5 8 3.2
9
8
9.5
8.5
9
8.5
9.5
4.1
Tomasz Wojaczyk
Tufts
University
8
8
7
8
7
8 7 2.6
5 4 5 6 7 5 5 2.9
8
6
6
7
8
7
6
3.6

【问题讨论】:

  • 不清楚你真正想要什么输出。您希望 [student] 姓名在一行中吗?你想在一行中写上学校的名字吗?你想让每个数字在单独的行上吗?也许您可以edit您的问题并发布您希望输出显示的方式。您不需要发布整个输出,只要清楚即可。

标签: java file output


【解决方案1】:

我想你必须从你的打印语句中取出所有新的行元素\n

System.out.printf("%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +"%s" + " " +", name, school, s1, s2, s3, s4, s5, s6, s7, diff);

【讨论】:

    【解决方案2】:

    在每次迭代中,您的代码都会读取两个整行元素加上八个数字,但您的文件每个块有 24 个数字。

    【讨论】:

      【解决方案3】:

      按照JavadocScanner::next 从这个扫描器中找到并返回下一个完整的令牌。Scanner::nextLine 使这个扫描器前进到当前行并返回被跳过的输入

      在您的情况下,输入是 重复 2 行字符串(姓名和学校)和 3 行数字(每行 8 个数字)。您已选择使用nextLine 读取两行字符串(姓名和学校),使用next 读取所有数字。以下是您分享的示例代码中的问题,我们将修复:

      1. 在每次迭代中,读取姓名和学校后,您需要读取 24 个数字,而不仅仅是 8 个数字。
      2. 由于您混合使用nextLine(使用换行符\n)和next(不使用换行符\n),因此您需要小心并使用所有换行符@ 987654332@妥妥的。
        例如。读完 8 个数字后,你仍然需要读一个换行符 \n。此新行将在下一次调用 nextLine 时使用(将扫描仪推进到当前行并返回被跳过的输入)。如果在读取 8 个数字后未调用 nextLine,则扫描仪将不会在输入这 8 个数字后消耗换行符 \n。当您尝试通过调用 nextLine 在下一次迭代中读取名称时,它会按照您的预期使用前一个换行符而不是包含下一个名称的行。
      3. \nprintf 的最后缺少,因此下一次迭代中的名称将打印在换行符上。

      修复上述所有 3 点后,您的代码应如下所示:

      File f = new File("divers.dat");
      Scanner kb = new Scanner(f);
      
      while (kb.hasNext()) {
          
          String name = kb.nextLine();
          String school = kb.nextLine();
          System.out.printf("%s\n%s\n", name, school);
      
          for (int i = 0; i < 3; i++) { // fix for point#1, read 24 numbers, not just 8
              String s1 = kb.next();
              String s2 = kb.next();
              String s3 = kb.next();
              String s4 = kb.next();
              String s5 = kb.next();
              String s6 = kb.next();
              String s7 = kb.next();
              String diff = kb.next();
              kb.nextLine(); // fix for point#2
      
              System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", s1, s2, s3, s4, s5, s6, s7, diff);
              // fix for point#3 -------------------------------^
      
              // use this if all numbers are not required on new line, however
              // I would suggest to use nextLine() similar to name and school
              // instead of multiple next calls
              // System.out.printf("%s %s %s %s %s %s %s %s\n", s1, s2, s3, s4, s5, s6, s7, diff);
          }
      }
      

      参考资料:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-07
        • 1970-01-01
        • 2019-09-04
        • 1970-01-01
        相关资源
        最近更新 更多