簡體   English   中英

我必須輸入名字、姓氏、學生 ID 和 avg,並將其顯示在新 csv 文件中以逗號分隔的行上。 我該怎么做呢?

[英]I have to enter a first name, last name, student id, and avg and have it displayed on a line separated by commas on a new csv file. How do I do this?

這是我更新的代碼。 同樣,說明如下:“輸入名字、姓氏、學生 ID 和 avg,然后將這 4 項內容顯示在一個新的 csv 文件中,每條記錄中的 4 個輸入中的每一個都用逗號分隔。” 這段代碼運行良好,有什么我可以做得更好的嗎? 另外,在這種情況下是否需要“in.close()”,因為我不是在讀取文件,而是在讀取用戶輸入?

public class Homework07 {
public static void main(String[] args) throws FileNotFoundException {
    System.out.println("Welcome! This program will store student records that you enter.");
    System.out.println("When you are done entering student records, simply type in 'Done' .");
    Scanner in = new Scanner(System.in);
  
    PrintWriter outFile = new PrintWriter("students.csv");
    
    while (true) {
        System.out.print("Please enter the first name: ");
        String firstName = in.nextLine();
        if (firstName.equals("Done")) {
            break;
        }
        System.out.print("Please enter the last name: ");
        String lastName = in.nextLine();
        System.out.print("Please enter the student ID: ");
        int studentId = in.nextInt();
        System.out.print("Please enter the current average: ");
        double currentAvg = in.nextDouble();
        in.nextLine();
        String newRecord = (firstName + ", " + lastName + ", " + studentId + ", " + currentAvg);
        
        outFile.println(newRecord); 
    }
    
    in.close();
    outFile.close();
}

}

你的代碼捕獲了用戶輸入的回車鍵,我添加了一個空的in.nextLine(); 為了逃避它,其次我添加了outFile.flush(); 將流刷新到文件中。

System.out.println("Welcome! This program will store student records that you enter.");
System.out.println("When you are done entering student records, simply type in 'Done' .");
Scanner in = new Scanner(System.in);

PrintWriter outFile = new PrintWriter("students.csv");

while (true) {
    System.out.print("Please enter the first name: ");
    String firstName = in.nextLine();
    if (firstName.equals("Done")) {
        break;
    }
    System.out.print("Please enter the last name: ");
    String lastName = in.nextLine();
    System.out.print("Please enter the student ID: ");
    int studentId = in.nextInt();
    System.out.print("Please enter the current average: ");
    double currentAvg = in.nextDouble();
    in.nextLine();
    outFile.write(firstName + "," + lastName + "," + studentId + "," + currentAvg);
    outFile.flush();
}

in.close();
outFile.close();

您對Scanner#nextDouble()方法的調用不會消耗ENTER鍵命中(換行符),因此您需要通過放置以下行來自己完成: in.nextLine(); 直接此行下:

double currentAvg = in.nextDouble();

具有諷刺意味的是,當您為要保存的每條記錄寫入文件時,您需要應用換行符,如下所示:

String record = new StringBuilder(firstName).append(", ").append(lastName)
                    .append(", ").append(studentId).append(", ")
                    .append(currentAvg).append(System.lineSeparator())
                    .toString();
outFile.write(record);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM