【问题标题】:Java : reading data and comparing then writingJava:读取数据并比较然后写入
【发布时间】:2016-01-18 06:10:39
【问题描述】:

我正在编写一个程序来从文本文件中读取数据(成绩),计算平均值,然后比较每个成绩并将数据分为两类:好和差(取决于低于或高于平均水平)

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream ("scores.txt");
    Scanner read = new Scanner (fis);
    int count=0, id;
    double total=0,average, grade;
    String line;
    Scanner stringScanner;
    while(read.hasNextLine()){
        line = read.nextLine();
        stringScanner = new Scanner(line);
        id = stringScanner.nextInt();
        while (stringScanner.hasNextDouble()){
            total = total + stringScanner.nextDouble();
            count++;
        }
        stringScanner.close();
    }
    average = total/count;
    System.out.println("The average is : "+ average);
    read.close();

    fis = new FileInputStream ("scores.txt");
    read = new Scanner (fis);
    while(read.hasNextLine()){
        line=read.nextLine();
        stringScanner=new Scanner (line);
        id = stringScanner.nextInt();
        while (stringScanner.hasNextDouble()){
            grade = stringScanner.nextDouble();
            if(grade >= average){
                FileOutputStream fos = new FileOutputStream("good.txt");
                PrintWriter pr = new PrintWriter (fos);
                pr.print(id + "/t"+ grade);
                pr.println();
            }
            else if (grade<average){
                FileOutputStream fos = new FileOutputStream("poor.txt");
                PrintWriter pr = new PrintWriter (fos);
                pr.print(id + "/t"+ grade);
                pr.println();
            }
            else System.out.println("test");
        }
        stringScanner.close();
    }
    fis.close();
    read.close();
    System.out.println("Files have been created...");
}

我认为我在计算平均值方面做得很好(因为它输出正确)。但是,我在编写糟糕和良好的文本文件时遇到了麻烦,这些文件已创建但它们仍然为空。我怎样才能让程序写在这两个文件上,我的代码到底缺少什么? 谢谢。

编辑: 下面是 .txt 文件的样子:

206527     44.24
208530     75.38
207135     85.61
205241     91.51
204324     50.61
203357     68.28
202117     57.11

【问题讨论】:

  • 能否提供txt文件!
  • 已添加,更新:我刚刚使用数组解决了它。但是,我仍然很好奇,因为讲师可能需要以下内容:通过两次读取文件而不使用数组来解决。它发生了。 :)
  • 我在下面提供的解决方案不正是您想要的吗?

标签: java if-statement fileinputstream fileoutputstream printwriter


【解决方案1】:

简而言之:这是因为您没有在PrintWriter上致电close()。除此之外,如果您每次想要添加内容时都不打开文件,它显然会更有效。在开始时打开这两个文件一次,然后写入它们。完成工作后,请记住关闭已打开的所有内容。以下是您程序的第二部分:

fis = new FileInputStream("scores.txt");
read = new Scanner(fis);
FileOutputStream fosGood = new FileOutputStream("good.txt");
PrintWriter prGood = new PrintWriter(fosGood);
FileOutputStream fosPoor = new FileOutputStream("poor.txt");
PrintWriter prPoor = new PrintWriter(fosPoor);
while (read.hasNextLine()) {
    line = read.nextLine();
    stringScanner = new Scanner(line);
    id = stringScanner.nextInt();
    while (stringScanner.hasNextDouble()) {
        grade = stringScanner.nextDouble();
        if (grade >= average)
            prGood.println(id + "\t" + grade);
        else if (grade < average)
            prPoor.println(id + "\t" + grade);
        else System.out.println("test");
    }
    stringScanner.close();
}
prGood.close();
prPoor.close();
fis.close();
fosGood.close();
fosPoor.close();
read.close();
System.out.println("Files have been created...");

此外,您不应从 main 方法中抛出任何异常,而应将代码放在 try/catch/finally 块中。在这种情况下,所有有关关闭文件的代码都将进入finally。您也可以选择使用try-with-resources statement

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多