0

I get a java.util.InputMismatchException when I read the following information from a file:

ADDRECORD CPCS-204 289 Rayyan Alzahrani 62 58 76

Student s = null;
while (input.hasNext()) {
    String command = input.next();
    if (command.equals("ADDRECORD")) {
        int ID = input.nextInt();
        String firstName = input.next();;
        String lastName = input.next();
        int[] examGrades = new int[nnmCourses];
        for (int i = 0; i < nnmCourses; i++) {
            examGrades[i] = input.nextInt();
            s = new Student(ID, firstName, lastName, examGrades);
            System.out.print(nnmCourses);
        }
1
  • On which line does that error occur? And it's not a compiler error. Commented Oct 19, 2015 at 22:19

1 Answer 1

3

You are calling

input.nextInt()

but the next token after ADDRECORD will be

CPCS-204

You need to read this token first, then read the ID field.

You might find it easier to use input.nextLine(), and then split or otherwise tokenize that string.

Sign up to request clarification or add additional context in comments.

Comments

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.