簡體   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