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

如何扫描文本文件并在java中输出以下内容?,java,algorithm,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

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

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 92 86

约翰逊·丹尼尔Y 73 56 83 82 69 72 61

威廉姆斯大卫N 60 70 91 56 70 71 77

琼斯卡罗尔N 88 85 76 68 61 84 98

布朗詹姆斯Y 67 91 62 73 74 83 94

Davis Dorothy N 96 60 97 58 82 100 89

米勒杰夫N 80 74 74 68 64 90 87

威尔逊威廉N 61 83 96 59 67 68 60

摩尔贝蒂N 65 59 93 86 72 80 73

泰勒海伦N 94778368806073

安德森·桑德拉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真的很陌生。有人能帮忙吗?

你可以试试这个代码片段,我没有正确理解你的问题。但最好使用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();
}

}
  }
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
公开课作业3{
公共静态void main(字符串[]args){
try(BufferedReader br=new BufferedReader(new FileReader(“C:\\students.txt”)){
弦线;
int lineNumber=1;
而((line=br.readLine())!=null){
系统输出打印项次(行);
String[]words=line.split(“”);

如果(words.length),您的问题本质上是:
“这里有一些广泛的要求和一些代码”
仅此而已。这类问题很难很好地回答,通常会被关闭。请尝试提出一个更具体和可回答的问题,您可能会得到一个体面和具体的答案。此外,您是新来的,因此请浏览、和部分,了解此网站的工作方式,并帮助您改进我们当前和未来的问题,从而得到更好的答案。如果你能用一个例子来解释你想要的结果,这是非常有用的。“Smith Richard N 87 98 59 77 92 86”您需要此记录的输出是什么。我需要的输出位于顶部谢谢您的帮助,但这给了我此错误输出。java.io.FileNotFoundException:students.txt(无此类文件或目录)位于java.io.FileInputStream.open(本机方法)位于java.io.FileInputStream。(FileInputStream.java:138)位于java.io.FileInputStream。(FileInputStream.java:93)位于java.io.FileReader。(FileReader.java:58)位于Assignment3.main(Assignment3.java:10)对于FileNotFoundException:students.txt,将文件路径指定给------new FileReader(“C:\\students.txt”)----[C:\\students.txt-将此路径更改为您的文件]通过填鸭式的回答,你剥夺了原始海报为自己发现代码的机会,这是对原始海报的一种伤害,你鼓励他们来这里寻找完整的解决方案,而不是首先自己思考。我使用的是mac和eclipse。因此,我将文件放在默认位置。它与我的项目一起工作.我会再试一次.满是鳗鱼的气垫船很抱歉,我也在考虑这个问题。