Results 1 to 6 of 6
- 11-27-2010, 02:37 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Scanner user input not working in conjunction with while loop.
I'm sure I'm doing something completely idiotic but this is stumping me. I'm having the same issue with another program and have no clue where to go from here.
When I comment out the while loop it works perfectly. With the while loop active, if I enter a number that shouldn't activate it, string2 is not printed but I'm given a second Scanner input box that shouldn't be there. If I do trigger the while loop, it prints string2 but then the same problem - it gives me a second, unnecessary Scanner input box.Java Code:Scanner s = new Scanner(System.in); System.out.print(string1); while(s.nextInt() < 10) System.out.print(string2); int n = s.nextInt();
Can anyone help me? Please!
Thanks!
-NadzLast edited by TheNadz; 11-27-2010 at 02:45 AM.
-
What is the purpose of this code? Can you show a small compilable program that demonstrates your problem? It preferably will have a small main method and nothing else.
edit: and what do you mean by "scanner input box"?Last edited by Fubarable; 11-27-2010 at 02:46 AM.
- 11-27-2010, 02:46 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Can do. Give me a second to get one together.
EDIT: Here you go:
Java Code:import java.util.*; public class test { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("What n would you like to go up to?"); while(s.nextInt() < 10) System.out.print("How about we try a bigger number, eh?"); int n = s.nextInt(); System.out.print(n); } }Last edited by TheNadz; 11-27-2010 at 02:48 AM.
-
I would use a do-while loop since you have to run the loop at least once. Also, you must take care not to discard a desirable result as you do in your code above. If the user enters a number > 10, you get the number here:
but you don't save it. That's why the user is forced to enter another number.Java Code:while(s.nextInt() < 10)
e.g.,
Also, you will want to enclose your while loop or do-while loop (or any loop or block) in curly braces. It may not seem important, but it will prevent some dumb mistakes (speaking from experience).Java Code:Scanner s = new Scanner(System.in); int testInt = 0; do { System.out.print("Enter an number greater than 9: "); testInt = s.nextInt(); } while (testInt < 10); System.out.println("you entered: " + testInt);
- 11-27-2010, 03:17 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Thanks! I'll give that a go.
And yeah, I tend to be lazy with the one line loops but it would be wise to use the brackets.
-Nadz
-
Great. Please let us know how it works out.
and I used to be the same way, but no more. I find that using curly braces always makes for less work later, and so is a smarter way to be lazy.And yeah, I tend to be lazy with the one line loops but it would be wise to use the brackets.
Similar Threads
-
while loop bypasses scanner input on 2nd pass
By xf021209 in forum New To JavaReplies: 2Last Post: 02-28-2010, 08:10 AM -
Scanner Not working
By Xystus777 in forum New To JavaReplies: 5Last Post: 03-18-2009, 12:22 AM -
[SOLVED] User Input - loop
By new person in forum New To JavaReplies: 4Last Post: 02-22-2009, 10:02 PM -
loop when there is no user-input
By becky in forum New To JavaReplies: 12Last Post: 02-02-2009, 10:02 PM -
Scanner Issues (User Input: Very Simple)
By carlodelmundo in forum New To JavaReplies: 8Last Post: 10-31-2008, 02:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks