简体   繁体   中英

Reading Data from a txt File in Java

I am trying to read data from a text file in Java. I want to read student names from the text file and put them into a String array which is called as "student". Also i want to read student grades. When i execute the program the output be like this: click to see the output

Please help me. How can i read student names and grades from the text file without using Buffer or Stream or etc.?

File file = new File("StudentScores.txt");
    
    Scanner input = new Scanner(file);
    
    int i = 0;
    while(input.hasNext()){
        input.nextLine();
        i++;
    }
    
    String[] student = new String[i];
    
    while(input.hasNext()){
        String name = input.next();
        double grade = input.nextDouble();
        System.out.println(name + " " + grade );
    }
    
    for(String a : student){
        System.out.println(a);
    }

StudentScores.txt Content:

John 60 70 80 90 65 75 70 89.5 75.4
Can 60 70 80 90 80 75 70 89.5 75.4
Cannot 60 -70 80 90 80 75 -70 89.5 75.4

Scanner moves only forward. First scanner part read whole file and second while(input.hasNext()) subpart will be not called. Take a look below script.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("StudentScores.txt");

        List<Student> students = new ArrayList<>();

        Scanner input = new Scanner(file);
        while (input.hasNext()) {
            String line = input.nextLine();
            if (line.isEmpty()) {
                break;
            }

            // split with spaces
            String[] str = line.split(" ");

            // first item is name
            Student student = new Student(str[0]);

            // visit rest items are grade values
            for (int i = 1; i < str.length; i++) {
                student.addGrade(Double.parseDouble(str[i]));
            }

            // add into the list
            students.add(student);
        }

        for (Student student : students) {
            System.out.println(student);
        }
    }

    private static class Student {
        private String name;
        private List<Double> grades = new ArrayList<>();

        public Student(String name) {
            this.name = name;
        }

        public void addGrade(double grade) {
            grades.add(grade);
        }

        public List<Double> getGrades() {
            return grades;
        }

        @Override
        public String toString() {
            return name + ", grades=" + grades;
        }
    }
}

Output:

John, grades=[60.0, 70.0, 80.0, 90.0, 65.0, 75.0, 70.0, 89.5, 75.4]
Can, grades=[60.0, 70.0, 80.0, 90.0, 80.0, 75.0, 70.0, 89.5, 75.4]
Cannot, grades=[60.0, -70.0, 80.0, 90.0, 80.0, 75.0, -70.0, 89.5, 75.4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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