简体   繁体   English

Java 读取文件后打印文件的行

[英]Java Printing the lines of a File after reading it

I'm just a beginner java programmer and I have no idea on how to print lines after the file is read.我只是一个初学者 java 程序员,我不知道如何在读取文件后打印行。

I got a text file like this我有一个像这样的文本文件

  • Jordan House 87.4约旦之家 87.4
  • Anthony Mason 76.5安东尼梅森 76.5
  • Austin Twumasi 90.5奥斯汀图马西 90.5
  • Ciprian Rhodes 66.9西普里安罗德 66.9
  • Darrell Poon 75.2达雷尔潘 75.2

and I was just wondering how will I print line whose grades are above 70.30我只是想知道如何打印成绩高于 70.30 的线

here is my code这是我的代码

 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);
            }



    }
}

thank you谢谢你

I would just use a simple if-statement.我只会使用一个简单的 if 语句。 Seems like you're using more code than needed.似乎您使用的代码比需要的多。 While loops wouldn't work the best for this case, though I guess you can use them!虽然循环在这种情况下效果不佳,但我想你可以使用它们!

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

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

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