Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Problem with Scanning input

  1. #1
    jajaja18 is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Question Problem with Scanning input

    Hey guys! I'm learning Java and I have to solve one problem:
    Write a method called inputBirthday that accepts a Scanner for the console as a parameter and prompts the user to enter a month, day, and year of birth, then prints the birthdate in a suitable format.
    This is how it should look in a console:
    Java Code:
    On what day of the month were you born? 8
    What is the name of the month in which you were born? May
    During what year were you born? 1981
    You were born on May 8, 1981. You're mighty old!
    This is my code:
    Java Code:
    public static void inputBirthday(Scanner console)
    {
        System.out.print("On what day of the month were you born? ");
        int day = console.nextInt();
        
        String month;
        System.out.print("What is the name of the month in which you were born? ");
        month = console.nextLine();
        
        System.out.print("During what year were you born? ");
        int year = console.nextInt();
        
        System.out.println("You were born on " + month + " " + day + ", " + year + ". You're mighty old!");
    }
    And the problem is that the program promts the user to enter day, but when it comes to month it doesn't and automatically prints "During what year were you born? ".

    What is wrong with this code? How to make it promt the user to enter month?

    PS The site on which I'm doing these problems wants me to write only the method with (Scanner console).

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with Scanning input

    The nextInt() method doesn't read the end of line character(s) because it/they can never be part of an int. This confuses the nextLine() method, i.e. it reads an empty line. The cure is simple: after having read something that leaves the end of line character(s) in the input buffer, read them with an additional nextLine() call (simply ignore its return value).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Problem with Scanning input

    Of course you can also create a utility method to do it for you, but perhaps that doesn't fit into the limits of your assignment:

    Java Code:
    public static int readSingleInt(Scanner console){
      int ret = console.nextInt();
      console.nextLine();
    
      return ret;
    }
    Still, it pays off to know you can make your life that tiny bit easier.

    BTW: "You were born on May 8, 1981. You're mighty old!" - I guess I'm old then :(
    Last edited by gimbal2; 08-13-2013 at 10:01 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jajaja18 is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with Scanning input

    Thanks you very mich JosAH. console.nextLine() worked like System.out.println(); and my solution wasn't accepted, but thanks to you I've managed by changing the int day = ... line to:
    Java Code:
    int day = Integer.parseInt(console.nextLine());

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,504
    Rep Power
    5

    Default Re: Problem with Scanning input

    Quote Originally Posted by gimbal2 View Post
    BTW: "You were born on May 8, 1981. You're mighty old!" - I guess I'm old then :(
    You're just a bunch of young'uns! ;)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem with Scanning input

    And I'm decrepit ...
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with Scanning input

    <from the grave> Silence! </from the grave>

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Problem with Scanning input

    Quote Originally Posted by JosAH View Post
    <from the grave> Silence! </from the grave>
    Jos wins.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem with Scanning input

    Quote Originally Posted by gimbal2 View Post
    Jos wins.
    Hands down, and (6) feet down.
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Scanning a txt file with Java?
    By maskanio in forum New To Java
    Replies: 1
    Last Post: 11-01-2012, 10:53 PM
  2. Problem with scanning char
    By danthegreat in forum New To Java
    Replies: 4
    Last Post: 09-09-2011, 07:01 PM
  3. pixel scanning too slow
    By crikey in forum Advanced Java
    Replies: 5
    Last Post: 08-21-2010, 03:49 AM
  4. Scanning in java
    By Waqas in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2009, 08:38 AM
  5. Java Twain Scanning
    By mwalstra in forum Advanced Java
    Replies: 3
    Last Post: 12-09-2008, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •