繁体   English   中英

尝试从一个文本文件中读取多行,然后将结果打印到新的文本文件 java

[英]Trying to read multiple lines from a one text file then print out results to new text file java

我创建了这个程序,它从一个记录学生成绩和姓名的文本文件中获取输入,如下所示:

姓氏:名字:Test1:Test2:Test3

在此之后,程序读取它们并用分隔符“:”分隔它们。 我被困在如何让程序读取带有名称的原始文本文件的下一行。 它在阅读第一行后停止并崩溃。 代码如下。

import java.io.*;
import java.util.Scanner;

public class exam {


    public static void main(String[] args) throws FileNotFoundException {
        String lastName;
        String firstName;
        int test1;
        int test2;
        int test3;
        Scanner input = new Scanner(new File("output.txt"));

        input.useDelimiter(":");

        while (input.hasNextLine()) {
            lastName = input.next();
            firstName = input.next();
            test1 = Integer.parseInt(input.next());
            test2 = Integer.parseInt(input.next());
            test3 = Integer.parseInt(input.next());


            //test print statements
            System.out.println(firstName + " " + lastName);
            System.out.println(test1);
            System.out.println(test2);
            System.out.println(test3);

            //averaging student letter grade

            double overallGrade = (test1 * .25) + (test2 * .30) + (test3 * .45);
            String letterGrade;
            System.out.println(overallGrade);

            //counter for letter grade
            int counterA = 0;
            int counterB = 0;
            int counterC = 0;
            int counterD = 0;
            int counterF = 0;


            if (overallGrade >= 90) {
                letterGrade = "A";
                counterA++;
            } else if (overallGrade >= 80) {
                letterGrade = "B";
                counterB++;
            } else if (overallGrade >= 70) {
                letterGrade = "C";
                counterC++;
            } else if (overallGrade >= 60) {
                letterGrade = "D";
                counterD++;
            } else {
                letterGrade = "F";
                counterF++;
            }

            try {
                BufferedWriter writer = new BufferedWriter(new FileWriter("filenamehere"));
                writer.write(lastName + ":" + firstName + ":" + test1 + ":" + test2 + ":" + test3 + ":" + overallGrade + ":" + letterGrade);
                writer.write("\nA " + counterA + "\nB " + counterB + "\nC " + counterC + "\nD " + counterD + "\nF " + counterF);

                writer.close();

            } catch (IOException e) {
                e.printStackTrace();
            }


            System.out.println(lastName + " " + firstName + " " + letterGrade);
        }


        input.close();
    }
}

您已经告诉扫描仪令牌用冒号分隔,然后您继续询问“下一个令牌之后是什么”。

问题是,您告诉扫描仪所有输入都用冒号分隔。 所以,不是换行符。 扫描程序尽职尽责地报告此文件中的第 5 个令牌:

lastName:firstName:Test1:Test2:Test3
foobar:baz:1:2:3

将是: "Test3\nfoobar" 毕竟,冒号之间的字符序列,不是吗? 您会看到这里如何涉及两个定界符:换行符分隔 2 条记录。 冒号分隔记录中的条目。 扫描仪不擅长这项工作。 所以,不要使用它。

让我们首先摆脱过时的文件 API 并使用新的,然后,让我们通过使用 try-with-resources 修复资源泄漏,它确保您打开的任何资源都绝对关闭(您编写的close()调用?它例如,如果发生异常,将不会被调用)。 让我们也修复你可悲的错误处理(永远不要捕获异常,执行e.printStackTrace() ,然后继续。这很讨厌 - 你不知道刚刚发生了什么,但代码会继续?当根据定义系统现在在一个 state 你没想到?这不是一个好主意)。 而且,当然,使用正确的工作工具 BufferedReader:它是一个比扫描仪更简单的 API。 这就是您想要的,我们将处理逐行拆分单个记录中的字段:

public static void main(String[] args) throws Exception {
  try (var in = Files.newBufferedReader(Paths.get("output.txt")) {
    processLine(in);
  }
}

static void processLine(String in) throws IOException {
  String[] parts = in.split(":");
  String lastName = parts[0];
  String firstName = parts[1];
  String test1 = Integer.parseInt(parts[2]);
  String test2 = Integer.parseInt(parts[3]);
  String test3 = Integer.parseInt(parts[4]);

  // rest of your code goes here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM