Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with my simple Scanner program?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with my simple Scanner program?

    Hello all,
    I am new to the forum and am seeking some help on why my program is not working as expected . For some reason, my program skips over letting the user enter their town. I would be very grateful for any help that one may provide me with:

    import java.util.Scanner;
     
    public class Okay
    {
      public static void main(String[] args)
      {
        String name, town;
        int age, siblings;
        Scanner details = new Scanner (System.in);
     
        System.out.println("Hey, what's your name?");
        name = details.nextLine();
     
        System.out.println("That's cool my friend is named " + name + " too!");
        System.out.println("How old are you?");
     
        age = details.nextInt();
        System.out.println("Wow, my friend, " + name + ", is " + age + " years old" +
                           " too!");
        System.out.println("What town do you live in?");
     
        town = details.nextLine();
     
        System.out.println("This is getting kind of crazy... My friend, " + name +
                           " lives in " + town + ", too!");
        System.out.println("How many siblings do you have?");
     
        siblings = details.nextInt();
     
        System.out.println("STOP RIGHT THERE. THIS IS INSANE. My friend, " + name +
                           ", has " + siblings + " siblings, too!!!");
     
        System.out.println("Wait, " + name + ", is that YOU!?");
     
     
      }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help with my simple Scanner program?

    That is because of the nextInt() method that you have used, you have read the number,
    but you have not skipped the line of the user's input,
    to avoid the bugs insert this code after you used nextInt() method.
    details.nextLine(); that will help your program to skip the line.
    hope that will help

  3. The Following User Says Thank You to dicdic For This Useful Post:

    helloworld9 (February 23rd, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my simple Scanner program?

    Thank you very, very much, adding details.nextLine(); fixed the problem!

  5. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Help with my simple Scanner program?

    Is this due to the newline('\0') being left in the buffer after the user enters in the int?

  6. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help with my simple Scanner program?

    the nextLine() method advances the scanner past the current line.
    I think the nextInt() method cannot do that..

    kindly see this link Scanner (Java Platform SE 7 )

Similar Threads

  1. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  2. [SOLVED] NoSuchElementException(), simple program not behaving right with Scanner class.
    By mwebb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:41 PM
  3. What's wrong with simple Scanner program?
    By SV25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 07:05 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM