如何扫描文本文件并在java中输出以下内容?

时间:2015-02-06 03:17:48

标签: java algorithm

对于从文件中读取的每个学生,输出以下信息:

Student Number (line number from the file, 1 to N)
Student Name (last name, a comma, and first name)
Student Athlete
Output Yes or No(Athlete Flag (Y = is an athlete, N = is not an athlete)
Eligibility-- Output either Yes or No --(Rule: If student is an athlete, their letter grade must be a “C” or higher to be eligible to play in the big game)

**这是文本文件的外观:

史密斯理查德N 87 98 59 77 77 92 86

Johnson Daniel Y 73 56 83 82 69 72 61

Williams David N 60 70 91 56 70 71 77

Jones Carol N 88 85 76 68 61 84 98

Brown James Y 67 91 62 73 74 83 94

Davis Dorothy N 96 60 97 58 82 100 89

Miller Jeff N 80 74 74 68 64 90 87

Wilson William N 61 83 96 59 67 68 60

Moore Betty N 65 59 93 86 72 80 73

Taylor Helen N 94 77 83 68 80 60 73

Anderson Sandra N 78 94 91 95 88 70 75

文本文件的每一行包含:

•学生姓氏

•学生名字

•运动员旗帜(Y =运动员,N =不是运动员)

•测验1等级

•测验2成绩

•测验3等级

•测验4成绩

•测验5等级

•考试成绩1

•考试成绩2

这是我到目前为止写的,

import java.io.*;
import java.util.*;

public class Assignment3 
{

    public static final void main(String args[]) throws FileNotFoundException
    {
        System.out.println("my name!");
        Scanner myScanner = new Scanner(new File("students.txt"));

        String firstName = myScanner.next();
        String lastName = myScanner.next();
        String athlete=myScanner.next();
            int lineNumber =1;
             while (myScanner.hasNextLine())
             {
                 if(myScanner.hasNextLine())
                 {
                    myScanner.nextLine();

                 }
            System.out.println(lineNumber + " "+ lastName + ", "+ firstName +
            athlete);

                String firstName = myScanner.next();
                String lastName = myScanner.next();
                String athlete=myScanner.next();
                lineNumber++;  
            }
   }
}

我是java的新手。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用此代码段,但我没有正确理解您的问题。但是使用BufferedReader而不是Scanner

会更好
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


  public class Assignment3 {

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader("C:\\students.txt"))) {

  String line;
  int lineNumber = 1;
  while ((line = br.readLine()) != null) {
    System.out.println(line);


    String[] words = line.split(" ");

    if (words.length <= 1) {
      continue;
    }

    String studentNumber = ""+ lineNumber++;
    String studentName = words[1]+","+words[0];
    String studentAthlete = words[2];


    boolean eligibility  = false;

    if(studentAthlete.equals("Y")){
        int totalMarks = 0;

       int subject = words.length-3;

        for (int i = 3; i < subject; i++) {
            totalMarks+= Integer.parseInt(words[i]);
        }

        int marksGrade = totalMarks/subject;

      //            now you can check the grade
        if(marksGrade<50){
            eligibility = true;
        }
    }

  }

} catch (IOException e) {
  e.printStackTrace();
}

}
  }