Results 1 to 13 of 13
- 03-03-2011, 04:00 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
Problems with While loop execution
I have a problem with the second while loop shown below. The guidelines I was given was that my variable 'speed' cannot be a negative number. So I make this a condition in my while loop. If a negative number is entered, it prompts the user to enter a number greater than or equal to 0. That's all fine and dandy...my problem is that if the user inputs a non-negative number it is ignored. Am I missing something??
Here is the part of code in question:
Any advice is greatly appreciated!Java Code:public class Demo { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the speed: "); while(!in.hasNextInt()){ System.out.println("Error: That was not a number!" + " '" + in.nextLine() + "'" + "\nEnter the speed: "); } int speed = in.nextInt(); while (speed < 0) { System.out.println("Enter a number greater or equal to 0: " + in.nextLine()); } SpeedHours speedHours = new SpeedHours(speed); System.out.println("The speed is: " + speedHours.getSpeed()); } }
- 03-03-2011, 04:02 AM #2
Where do you assign a new value to speed?
- 03-03-2011, 04:08 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
I was just going to say that I tried that, but maybe I didn't do it correctly. After you mentioned that, I redefined "speed" and ran it. It works. I'm such a noob that it's obvious! Thank you much!
- 03-03-2011, 04:11 AM #4
Are you sure it works?
What happens if I enter: -10 dammit
- 03-03-2011, 04:14 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
It definitely works! I put -10 in and it prompted me for a number greater than or equal to 0, so I put -10 in again. Same result. Then I put in 5, and it broke cleanly!
- 03-03-2011, 04:15 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
Now I can move on to the next segment of loops that is very similar. Then I'll have to use a GUI to output all the information.
- 03-03-2011, 04:16 AM #7
But what about my input?
-10 dammit
That will crash your program.
- 03-03-2011, 04:20 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
-10 didn't crash my program at all. This is the result:
Java Code:Enter the speed: -10 Enter a number greater or equal to 0: -10 Enter a number greater or equal to 0: -10 Enter a number greater or equal to 0: 5 The speed is: 5 BUILD SUCCESSFUL (total time: 9 seconds)
- 03-03-2011, 04:22 AM #9
GAH!
Can you see past the -10 to the dammit? I'm saying try enterering "-10" as the first input and then "dammit" as the second input.
- 03-03-2011, 04:37 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
Junky...sorry it took me awhile to realize what you were asking. I edited my code to look like this:
And this was the result:Java Code:public class Demo { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the speed: "); while(!in.hasNextInt()){ System.out.println("Error: That was not a number!" + " '" + in.nextLine() + "'" + "\nEnter the speed: "); } int speed = in.nextInt(); while (speed < 0) { System.out.println("Enter a number greater or equal to 0: " + in.nextLine()); while (!in.hasNextInt()){ System.out.println("Error: That was not a number!" + " '" + in.nextLine() + "'" + "\nEnter the speed: "); } speed = in.nextInt(); } SpeedHours speedHours = new SpeedHours(speed); System.out.println("The speed is: " + speedHours.getSpeed()); } }
Java Code:Enter the speed: -10 Enter a number greater or equal to 0: dammit Error: That was not a number! 'dammit' Enter the speed: 5 The speed is: 5 BUILD SUCCESSFUL (total time: 11 seconds)
- 03-03-2011, 04:41 AM #11
OK you managed to fix your code but notice how your loop checking for non-numbers is repeated in your code. A further exercise can be to see how you can modify it to eliminate the repeated code. One way is to only use a single loop.
Java Code:while input is invalid { // get input and check it }
- 03-03-2011, 06:06 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
Okay...here is what I came up with. Is this what you were referring to?
I've added some other code with this. Hopefully it's not too confusing. If I overdid the comments let me know.Java Code:public class Demo { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Enter the speed: "); boolean invalidInput = false; // Checks the input of the user while(!invalidInput){ // If input is not an integer, prints error message and prompts user to re-enter. while(!in.hasNextInt()){ System.out.println("Error: That was not a number!" + " '" + in.nextLine() + "'" + "\nEnter the speed: "); } int speed = in.nextInt(); // If speed < 0, prints error message and prompts user to re-enter. // If correct input entered, int speed is redefined. while (speed < 0){ System.out.println("Enter a number greater than or equal to 0: " + in.nextLine()); speed = in.nextInt(); } // Prompts user to enter hours. System.out.println("Enter the hour(s): "); // If input is not an integer, prints error and prompts user to enter valid integer. while(!in.hasNextInt()){ System.out.println("Error: That was not a number!" + " '" + in.next() + "'" + "\nEnter the hour(s): "); } int hours = in.nextInt(); // If hours < 1, prints error message and prompts user to re-enter. // If correct input entered, int hours is redefined. while (hours < 1){ System.out.println("Enter a number greater than or equal to 1: " + in.nextLine()); hours = in.nextInt(); } // New object speedHours created. SpeedHours speedHours = new SpeedHours(speed, hours); // Outputs the data entered, and displays the distance travelled. System.out.println("The speed is: " + speedHours.getSpeed() + " miles per hour." + "\nThe time is: " + speedHours.getHours() + " hours." + "\nThe distance travelled is: " + speedHours.getDistance() + " miles."); } } }
- 03-03-2011, 07:35 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Applet Execution Problems
By vm8951 in forum Java AppletsReplies: 13Last Post: 12-01-2010, 06:06 AM -
Re-iterate Thread execution after its execution finishes.
By TurtleRock in forum New To JavaReplies: 10Last Post: 11-29-2010, 02:02 PM -
Loop problems
By jim01 in forum New To JavaReplies: 3Last Post: 10-18-2010, 12:49 AM -
External Program execution problems
By vital101 in forum Advanced JavaReplies: 3Last Post: 10-30-2007, 05:17 PM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks