0

What I'm trying to do is allow the user to enter a first name and a surname. The user is allowed to enter a first name with no issue but as soon as space is hit and their surname is entered, it gives me the error mentioned in the title.

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int choice, 
        String name, stuNum;

        do
        {   
            System.out.println("");
            System.out.println("Student Grade System");
            System.out.println("----------------------\n");
            System.out.println("   1) Enter student details");
            System.out.println("   2) Display student grades");
            System.out.println("   3) Display student statistics");
            System.out.println("   4) Display full transcript");
            System.out.println("   0) Exit System\n");
            System.out.print("Select an option [0-4] >> ");
            choice = sc.nextInt();
            System.out.println("");

            switch(choice)
            {
                case 1:
                System.out.println("Entering Student Details");
                System.out.println("------------------------");
                System.out.print("   Student name:                               ");
                name = sc.next(); //somewhere here it messes up
                System.out.print("   Student number:                             ");
                stuNum = sc.next();
            }
         }while(choice !=0);
}

This is the bare minimum of my code as I am just starting to write it but this issue is preventing me from going further. I've already tried using sc.nextLine(); and changing the variable stuNum to an int variable. Neither works. The full error it gives is as follows:

java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at Project.main(Project.java:40)

3
  • try nextLine() instead of next() Commented Nov 3, 2014 at 17:46
  • I've stated that I already tried that. However, I will try again. Whereabouts were you thinking to replace it? at both variables? Or just the name variable? Commented Nov 3, 2014 at 17:47
  • I just tested your program on my machine and I'm not getting any error (except that there was a ',' after the definition of choice instead of a ;. Are you sure you're showing us the same version you have? Commented Nov 3, 2014 at 18:02

3 Answers 3

1

Using next() will only return what comes before a space.That means It won't consume a space. but nextLine() automatically moves the scanner down after returning the current line. So, just replace next() with nextline() in your code.

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

Comments

0

Use extra nextLine to flush the scanner buffer after you have taken students name

switch(choice)
            {
                case 1:
                System.out.println("Entering Student Details");
                System.out.println("------------------------");
                System.out.print("   Student name:                               ");
                name = sc.nextLine(); //somewhere here it messes up
sc.nextLine();  // to clean the enter key stored in the buffer
                System.out.print("   Student number:                             ");

                stuNum = sc.nextLine();
            }

1 Comment

Simple enough answer and it worked perfectly. Thanks for that.
0

This is because sc.next() will not consume the space. So it will read the next token (in this case the lastname after the space) instead of the next thing entered. Try for yourself: when you put in a name enter Firstname 1 and it will enter student number 1 automatically. Use Scanner's nextLine() method to consume the entire line.

You can parse your int choice with Integer.parseInt(sc.nextLine());

2 Comments

Okay. I replaced name = sc.next(); with name = sc.nextLine(); and all that did was place Student name: and Student Number: into one line when I ran it.
@SlowpPowl I recommend using nextLine() for any input for this type of step-by-step program. You can parse the line however you wish to fit any datatype.

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.