user5723057
user5723057

Reputation:

reading student record from file

I'm trying to read a file that has student record(first name, last name, and grade). I have written a simple code to accomplish this task but the code fails after reading two lines from the text file. Here is my code:

public class Student {
private final  String first,last;
final int MAXGRADE = 100; 
final int LOWGRADE = 0; 
private final int grade;

public Student(String firstname,String lastname, int grade){
    this.first = firstname;
    this.last = lastname;
    this.grade = grade;

}
@Override
public String toString(){
    return first + " " + last + "\t" + grade;
}
}

and the driver has this code

public class driver {
public static void main(String[] args) throws FileNotFoundException {
   String first_name ,last_name;
    int grade;
    Scanner fileInput = new Scanner(new File("data1.txt"));
    while (fileInput.hasNextLine())
    {
        first_name = fileInput.next();
        last_name = fileInput.next();
        grade = fileInput.nextInt();

        Student st = new Student(first_name, last_name,grade);

      System.out.println(st);

    }
}
}

the compiler is pointing to this

grade = fileInput.nextInt();

as the source of the error.

Upvotes: 0

Views: 2011

Answers (3)

Manu
Manu

Reputation: 27

This code is working for me. Make Sure

  1. The location of text file correctly given,
  2. Integer value given at 3rd position in each line (like:- steve smith 22)

Upvotes: 1

AxelH
AxelH

Reputation: 14572

From the comment you post "@AxelH each line represents a single student first, last name and grade" we can see the problem.

Your actual loop to read a line

while (fileInput.hasNextLine())
{
    first_name = fileInput.next();
    last_name = fileInput.next();
    grade = fileInput.nextInt();

    Student st = new Student(first_name, last_name,grade);

  System.out.println(st);

}

Is reading 3 lines, one per fileInput.nextXXX();. What you need to do is

  • Read a line as a String : `String line = fileInput.nextLine();
  • Split that line base on the delimiter : `String[] data = line.split(" "); //Or your delimiter if not space (carefull with some names...)
  • Set the value from this array (parse are need for integer)

EDIT :

I have made a mistake since I am used to use nextline and not next, I can't delete the answer as it is accepted so I will update it to be more correct without changing the content.

The code is indeed correct, next will take the following input until the next delimiter, \\p{}javaWhitespace}+, but using the given solution would give you more solution to manage composed names as it could be Katrina Del Rio 3.

Upvotes: 0

Shyam Baitmangalkar
Shyam Baitmangalkar

Reputation: 1073

If you are using Java 8, then functional way of doing this would be:

String filePath = "C:/downloads/stud_records.txt"; // your file path
        /*
         * Gives you a list of all students form the file
         */
        List<Student> allStudentsFromFile = Files.lines(Paths.get(filePath)).map(line -> {
            String[] data = line.split("\\s+"); //Split on your delimiter
            Student stud = new Student(data[0], data[1], Integer.parseInt(data[2]));
            return stud;
        }).collect(Collectors.toList());

Note: I've made an assumption that this:

FirstName LastName Grade

is the input file format.

Upvotes: 0

Related Questions