Results 1 to 5 of 5
- 03-25-2010, 03:46 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
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 addedLast edited by Fubarable; 03-25-2010 at 03:50 AM. Reason: Moderator Edit: Code tags added
-
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.
- 03-25-2010, 03:58 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
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?
- 03-25-2010, 07:28 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
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(); }
- 03-25-2010, 04:47 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Jcreator to DOS Prompt
By arshesander in forum New To JavaReplies: 5Last Post: 02-17-2010, 06:25 AM -
how to get the values from command prompt
By tej in forum New To JavaReplies: 3Last Post: 05-02-2009, 08:32 AM -
compiling with comand prompt
By gogledgeak in forum New To JavaReplies: 4Last Post: 04-14-2009, 03:28 AM -
help me!!!! about command prompt..
By kureikougaiji in forum New To JavaReplies: 2Last Post: 11-13-2008, 06:15 PM -
input prompt
By angelbaby21 in forum New To JavaReplies: 8Last Post: 08-25-2008, 04:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks