Results 1 to 5 of 5
  1. #1
    G-Unit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default How to cause a second prompt to run after the first.

    I am working on an assignment for a first year Java course. For the program I am writing, I need to be able to ask the user for a card (as in deck of cards) size, and then ask them for a card suit. The program will then draw out the card suit in *'s. The printing is not my problem. This is the beginning of my main method:

    Java Code:
    do {
            
            System.out.println( "Which size card do you want? (0 or bigger): " );
            int size = scan.nextInt();
            if( size < 0 )
            {
                System.out.println( "Size was too small (" + size + ")." );
                size = 0;
                System.out.println( "I'll use " + size + " instead." );
               
            }
    
             System.out.println( "Which card suit? (diamonds, hearts, spades): ");
    		String suit = scan.nextLine();


    The problem is the program runs through the first prompt: "Which size card do you want? (0 or bigger): ", then will print the second prompt: "Which card suit? (diamonds, hearts, spades): ", but it won't pause at the second prompt. It continues on to the next block statement. Any suggestions? Thanks in advance.

    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-25-2010 at 04:50 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What happens if you change this:
    Java Code:
            System.out.println( "Which size card do you want? (0 or bigger): " );
            int size = scan.nextInt();

    to this:
    Java Code:
            System.out.println( "Which size card do you want? (0 or bigger): " );
            int size = scan.nextInt();
            scan.nextLine();  // to allow the Scanner to "swallow" the end of line symbols.
    ?

  3. #3
    G-Unit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks so much, it works. I'm not sure I totally understand what is occuring here though:

    scan.nextLine(); // to allow the Scanner to "swallow" the end of line symbols.

    Does it scan the next line which is blank, and then pause?

  4. #4
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    you may want to change the first part for something like this:

    Java Code:
    System.out.println( "Which size card do you want? (0 or bigger): " );
    int size = scan.nextInt();
    scan.readLine(); // already suggested
    while(size < 0){
        System.out.println( "Size was too small (" + size + ")." );
        System.out.println( "Please choose another one, this time bigger than ZERO" );
        size = scan.nextInt();
        scan.readLine(); 
    }

  5. #5
    G-Unit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Jcreator to DOS Prompt
    By arshesander in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:25 AM
  2. how to get the values from command prompt
    By tej in forum New To Java
    Replies: 3
    Last Post: 05-02-2009, 09:32 AM
  3. compiling with comand prompt
    By gogledgeak in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:28 AM
  4. help me!!!! about command prompt..
    By kureikougaiji in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 07:15 PM
  5. input prompt
    By angelbaby21 in forum New To Java
    Replies: 8
    Last Post: 08-25-2008, 05:22 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
  •