0

I have built this program that will read records from a file and build an array of students, based on a Student class.

**StudentTest Class

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

public class StudentTest 
{
    public static void main(String[] args) 
    {
    String name;
    String address;
    String major;
    double gpa;
    int classLevel;
    int college;
    String blank;
    String idNumber;

    Scanner fileIn = null;
    try
    {
        fileIn = new Scanner (new FileInputStream("student.dat"));
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File not found");
        System.exit(0);
    }

    Student[] aStudent = new Student[15];
    int index = 0;
    if (fileIn.hasNext())
    {
    for (index=0; index <= 15; index++)
    {
        blank = fileIn.nextLine();
        name = fileIn.nextLine();
        //System.out.println("Name: " + name);
        address = fileIn.nextLine();
        //System.out.println("Address: " + address);
        major = fileIn.nextLine();
        gpa = fileIn.nextDouble();
        //System.out.println("Major: " + major + " GPA: " + gpa);
        classLevel = fileIn.nextInt();
        college = fileIn.nextInt();
        //System.out.println("Class Level: " + classLevel + " college " + college);
        idNumber = fileIn.nextLine();
        aStudent[index] = new Student(name, address, major, gpa, classLevel, college, idNumber);
        //System.out.println("===================================");
        aStudent[index].Display();
    }
    }
    else
    {
    fileIn.close();
    }
   }
     }

** Student Class

    import java.util.Scanner;

    public class Student 
       {
       private String name;
       private String address;
       private String major;
       private double gpa;
       private int classLevel;
       private int college;
       private String idNumber;
       Scanner keyboard = new Scanner(System.in);

    public Student(String name, String address, String major, double gpa, int classLevel, int coll, String idNum)
    {
        this.name = name;
        this.address = address;
        this.gpa = gpa;
        this.major = major;
        this.classLevel = classLevel;
        this.college = coll;
        this.idNumber = idNum;

    }
    public String getName()
    {
        return name;
    }
    public String getAddress()
    {
        return address;
    }
    public String getMajor()
    {
        return major;
    }
    public double getGPA()
    {
        return gpa;
    }
    public int getClassLevel()
    {
        return classLevel;
    }
    public int getCollege()
    {
        return college;
    }
    public String getID()
    {
        return idNumber;
    }
    public void setAddress(String address)
    {
    }
    public void setMajor(String maj)
    {
    }
    public void setCollege(int coll)
    {
    }
    public void Display()
    {
        System.out.println("Name: "+getName());
        System.out.println("Address: "+getAddress());
        System.out.println("Major: " + getMajor());
        System.out.println("GPA: "+getGPA()+" Class Level: "+getClassLevel()+" College: "+getCollege());
        System.out.println("ID: "+getID());
        System.out.println("===============================");
    }
}

and here is the file (student.dat)

Doe John D 
123 Fake St
IS 
4.0 
4 
15
M123456789

Smith Thomas F 
345 Lakeside Ln 
ACCT 
3.0
2 
14
M235896135

When I run it, this is my output

Name: Doe John
Address: 123 Fake St
Major: IS 
GPA: 4.0 Class Level: 4 College: 15
ID: 
===============================
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at StudentTest.main(StudentTest.java:41)

Thanks

1
  • 3
    Okay. First off, you've provided way too much code. Second, this line: at StudentTest.main(StudentTest.java:41) refers to the exact class (StudentTest.java) and line number (41) where you're receiving the error. What line is this? Hint: I'm not about to count 41 lines. Commented Oct 1, 2013 at 0:06

1 Answer 1

1

Understand that one Scanner methods handle the end-of-line (EOL) token, and that's nextLine(), and all of the others don't. So if you call nextInt() a bunch of times and then call nextLine() the nextLine() call will grab the EOL token tangling from the previous nextInt() call. One solution, follow your nextInt() call with nextLine() just to swallow the EOL token.

For example, change this:

    college = fileIn.nextInt();
    idNumber = fileIn.nextLine();

to this:

    college = fileIn.nextInt();
    fileIn.nextLine();  // handle the EOL token
    idNumber = fileIn.nextLine();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That fixed that issue. The program is outputing both students now, but is still throwing an error "java.util.Scanner.hasNext(Unknown Source). I assume that I have an error in my looping. How should I resolve this?
@user1873736: myself, I couple every nextXXX() with a preceding hasNextXXX(), so there is little room for error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.