how can I read a text file, process it & write toString()

kanye :

I'm having a number of issues with this:

1) Under which class would I want to put my Scanner, so that it assigns the proper variables? The task I am given says to "read data file into Students" in the Tester class

2) How can I make a readFile() method in the Students class?

3) How can I properly write toString() in both Student and Students classes?

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

public class Tester
{
    public static void main(String args[]) throws IOException
    {
        //new Students object
        Students theStudents = new Students();

        //reads file from Students
        theStudents.readFile();

        // create new 'Output.txt' file to save program output
        PrintWriter ot = new PrintWriter(new FileWriter("Output.txt"));

        System.out.println(theStudents.toString());
        ot.println(theStudents.toString());

        ot.close();
    }
}

import java.util.ArrayList;
import java.io.*;     
import java.util.*;    
import java.io.FileReader;

public class Students
{
    // instance variables
    private ArrayList<Student> students;

    public Students()
    {
        //create arraylist for students
        students = new ArrayList<>();
    } 

    public void readFile() throws IOException
    {
        String name;
        int age;
        double gpa;

        String line;

        //Scanner to read file
        try {
        Scanner sc = new Scanner(new File("Students.txt"));
        while (sc.hasNextLine()) {
            name = sc.nextLine();

            line = sc.nextLine();
            age = Integer.parseInt(line);

            line = sc.nextLine();
            gpa = Double.parseDouble(line);
        }
        }
        catch (FileNotFoundException e) {
              System.out.println("Error");
        }
    }

    public void add(Student s)
    {
        students.add(s);
    }

    public Students aboveAverage(double avgGPA)
    {
        Students aboveAverage = new Students(); 

        for (int i = 0; i < students.size(); ++i) {
            if (students.get(i).getGPA() > avgGPA)
                aboveAverage.add(students.get(i));
        }
        return aboveAverage;
    }

    public String toString()
    {
        String out = "";

        int count = 0;
        for (Student student : students){
            out += students.toString() + " ";
            ++count;
        }

        return out;
    }
}

public class Student
{
    private String name;
    private int age;
    private double gpa;

    public Student(String studentName, int studentAge, double studentGPA)
    {
        name = studentName;
        age = studentAge;
        gpa = studentGPA;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public double getGPA()
    {
        return gpa;
    }

    public String toString()
    {
        return String.format("%10s", name) + 
               String.format("%5d", age) + String.format("%10.2f \n", gpa);
    }
}
user2004685 :

I'm not gonna give you the complete solution but here is a way to approach this problem.

  • You need readLine() instead of nextLine()
  • Once you read the values, you need to call the add() function with the Student Object to add it to your ArrayList.

Code Snippet:

try (Scanner sc = new Scanner(new File("Students.txt"))) {
    while (sc.hasNextLine()) {
      String name = sc.readLine();
      int age = Integer.parseInt(sc.readLine());
      double gpa = Double.parseDouble(sc.readLine());
      /* Create A New Student Object & Add To List */
      add(new Student(name, age, gpa));
    }
} catch (FileNotFoundException e) {
    System.out.println("Error");
}

Also, you need to @Override the toString() function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=102336&siteId=1