F. Moss
F. Moss

Reputation: 55

Using a delimiter on a scanner object passed to a method

This question is linked to another question on this site:

Beginner in Java - Using Parameters and Scanner

The practice site in question creates the class and calls the method you write using the following call: inputBirthday(new Scanner("8\nMay\n1981\n "));

I'm curious to know why it won't work either. My method code is this:

public static void inputBirthday(Scanner scan) {
    System.out.print("On what day of the month were you born? ");
    int monInt = scan.nextInt();
    System.out.print("What is the name of the month in which you were 
    born? ");
    String monStr = scan.nextLine();
    System.out.print("During what year were you born? ");
    int year = scan.nextInt();
    scan.close();
    System.out.println("You were born on " + monStr + ", " + monInt + year 
    + ". You're mighty old!");
}

I keep getting this error:

"NoSuchElementException: Near input line 3token 'May' cannot be interpreted as type int,"

right after this line in my code:

int year = s.nextInt();

Any ideas? Thanks!

Upvotes: 0

Views: 92

Answers (2)

Tezra
Tezra

Reputation: 8833

Parsing "8\nMay\n1981\n " with

    int monInt = input.nextInt(); // Reads 8
    System.out.print("What is the name of the month in which you were born? ");
    String monStr = input.nextLine(); // Reads to end of line, aka '\n'
    System.out.print("During what year were you born? ");
    int year = input.nextInt(); // reads next token as int, which is "May"

So the problem is that the nextLine() is reading the end of the first line, not Mays line.

Upvotes: 0

user2577576
user2577576

Reputation:

When you enter the day of the month you are born and press enter, i.e.

System.out.print("On what day of the month were you born? ");
    int monInt = scan.nextInt();

you are expecting them to enter a number. If they were to enter 13 and press enter, the computer would see that you had entered 13\n, as you have also pressed enter. Your call to nextInt() will read the 13, but leave the \n. The way that nextLine() works is that it reads until the next occurrence of \n, as \n is used to end a line. The issue with this is that now you are calling System.out.print("During what year were you born? "); int year = scan.nextInt(); next, however you are still expecting to enter a string representation of the month in which they were born. Thus, when you enter the String, your call to nextInt() cannot be parsed as an integer, because it is not an integer -- it is a String! You have two options to fix this -- you can either make a call to scan.nextLine() directly after your call to nextInt() the first time in order to strip the \n from the input buffer, or you can use scan.next() instead of scan.nextLine() to store the String representation of the month. next() will (usually) read until the next space, so the \n which still exists in the input buffer will not cause any issues. A functional version of your code would be the following --

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("On what day of the month were you born? ");
        int monInt = input.nextInt();
        System.out.print("What is the name of the month in which you were born? ");
        String monStr = input.next();
        System.out.print("During what year were you born? ");
        int year = input.nextInt();
        input.close();
        System.out.println("You were born on " + monStr + ", " + monInt + year
                + ". You're mighty old!");
    }

and an example output from this program would be

On what day of the month were you born? 13
What is the name of the month in which you were born? October
During what year were you born? 1943
You were born on October, 131943. You're mighty old!

Note that you still have to change the text formatting to get it to output exactly as you likely want, but the issue you were having before has been fixed.

Upvotes: 1

Related Questions