0

Currently programming an interactive program for class in Java; it's a menu with 5 different choices(0-4) through a switch statement. When started, a Scanner takes in whichever the choice is and executes one of five different methods.

case 2:
    System.out.println("Choose where you want to put the student");
    int id2 = scan.nextInt();
    AddStudent(id2);
    ViewStudents("all");
    break;

Which calls AddStudent:

private void AddStudent(int id) {
    System.out.println("Type in name, subject and mark: ");

    String name = scan.nextLine();
    String subject = scan.nextLine();
    int mark = scan.nextInt();

    /* check via loop studentNames array and if it's null at
     [i], set studentNames[i]  to name,
     studentSubjects[i] to subject and studentMarks[i] to mark. 
    */
    for (....)  {
       if (studentNames[i] == null) {
          studentNames[i] = name;
    }
}

But when I run this method I don't get to input the three scan instances. I get a InputMismatchException. How do I work around this?

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help) , in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). Please read (and follow) the [Java Naming Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) – Timothy Truckle Feb 10 '18 at 18:54
  • Try always using nextLine. Parse strings to the appropriate type – OneCricketeer Feb 10 '18 at 18:55

0 Answers0