Results 1 to 13 of 13
  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default 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:

    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());
        }
    }
    Any advice is greatly appreciated!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Where do you assign a new value to speed?

  3. #3
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    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!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Are you sure it works?

    What happens if I enter: -10 dammit

  5. #5
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    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!

  6. #6
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    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.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    But what about my input?

    -10 dammit

    That will crash your program.

  8. #8
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    -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)

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

  10. #10
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Junky...sorry it took me awhile to realize what you were asking. I edited my code to look like this:

    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());
        }
    }
    And this was the result:

    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)

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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
    }

  12. #12
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Okay...here is what I came up with. Is this what you were referring to?

    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.");
                }
    
          }
            
            
    }
    I've added some other code with this. Hopefully it's not too confusing. If I overdid the comments let me know.

  13. #13
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I just realized that it still crashes...I'll chaulk it up to not knowing what the hell I'm doing! haha

Similar Threads

  1. Applet Execution Problems
    By vm8951 in forum Java Applets
    Replies: 13
    Last Post: 12-01-2010, 06:06 AM
  2. Replies: 10
    Last Post: 11-29-2010, 02:02 PM
  3. Loop problems
    By jim01 in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:49 AM
  4. External Program execution problems
    By vital101 in forum Advanced Java
    Replies: 3
    Last Post: 10-30-2007, 05:17 PM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 07:19 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
  •