首页 > 解决方案 > Java在读取文件后打印文件的行

问题描述

我只是一个初学者 java 程序员,我不知道如何在读取文件后打印行。

我有一个像这样的文本文件

我只是想知道如何打印成绩高于 70.30 的行

这是我的代码

 public class Question1 
{
    public static void main (String [] args) throws IOException
    {
        // Create a Scanner object attached to the keyboard
        Scanner in = new Scanner (System.in);
        // Write your code here!!!!
        System.out.print ("Enter the input filename: " );
        String filename = in.nextLine();
        // Open the datafile
        File file = new File(filename);
        // set the delimited to be a ,
        Scanner inFile = new Scanner(file);
        double avgGrade = 0;
        double totalAvgGrade = 0;
        int numStu = 0;
        String firstName = null;
        String lastName = null;



        while (inFile.hasNext())  
        {
            firstName = inFile.next();
            lastName = inFile.next();
            String gradeAvg = inFile.next();
            avgGrade = Double.parseDouble(gradeAvg);
            totalAvgGrade += avgGrade;
            numStu++;


        }
        double average = totalAvgGrade/numStu;
        System.out.printf("%s %.2f \n","Average grade is", average);
        System.out.println("Students with grade > average ");
        System.out.printf("%s %10s %10s", "First", "Last", "Score");
        if ( avgGrade > average)
            {
                System.out.printf("\n %s %10s %.2f" ,firstName , lastName , avgGrade);
            }



    }
}

谢谢你

标签: java

解决方案


我只会使用一个简单的 if 语句。似乎您使用的代码比需要的多。虽然循环在这种情况下效果不佳,但我想你可以使用它们!

If(grades > 70) 
{
    System.out.println(*code code code*);
}

推荐阅读