3

I'm a noob, so I apologize if I'm asking a stupid question. I'm asking a user to input a numerical value. If the value is less than 12, or a non-numerical value such as a number, I want it to prompt them for another input. When a value of at least 12 is entered, I want that value to assigned to a variable called creditUnits;

When I ask for the initial prompt, my program will catch it if the value entered is non-numerical, it will ask the user to "Enter a valid number:". The while loop seems to work well.

I'm having an issue with the second loop, where it is supposed to catch any number entered in that is less than 12. It will ask the user to enter a value greater than 11. Problem I'm having is at this point if the user enters any value at this point, the program just sits there. Any help would be appreciated, and I apologize in advance for the sloppiness of my code:

System.out.print("Enter the credits's you will take each term: ");

while (!in.hasNextDouble()){
    System.out.print("Enter a valid number: ");
    in.next();
}

creditUnits = in.nextDouble();

if (creditUnits < 12){
    System.out.print("Enter a number greater than 11: ");
    in.next();
}

creditUnits = in.nextDouble();                       
System.out.println("You will be taking " + creditUnits + " credits per term.");    
3
  • 3
    There are no stupid questions. Commented Feb 19, 2014 at 21:34
  • Try replacing hasNextDouble() with hasNextInt(). By the way, I agree with Secator: there are no stupid questions; specifically, your question is not stupid at all ;) Commented Feb 19, 2014 at 21:35
  • user3330001 did my answer help? Commented Feb 21, 2014 at 16:17

2 Answers 2

4

It's because you're asking the scanner to grab the next TWO inputs, when you only want the first.

System.out.print("Enter the credits's you will take each term: ");

while (!in.hasNextDouble()){
    System.out.print("Enter a valid number: ");
    in.next();
}

creditUnits = in.nextDouble();

if (creditUnits < 12){
        System.out.print("Enter a number greater than 11: ");
        creditUnits = in.nextDouble();  
}


System.out.println("You will be taking " + creditUnits + " credits per term.")

Also, one thing you should consider is putting the if(creditUnits < 12) block in a while loop, so you can continually check if they entered a number greater than 12.

Something like:

System.out.print("Enter the credits's you will take each term: ");
while (true){
    System.out.print("Enter a valid number: ");
    creditUnits = in.nextDouble();
    if (creditUnits < 12){
        System.out.print("\nNumber must be greater than 12!\n"); 
    }else
        break;
}

System.out.println("You will be taking " + creditUnits + " credits per term.");

Also also, there is no such thing as a stupid question. Only stupid Flyers fans. /joke

Sign up to request clarification or add additional context in comments.

Comments

0

Like Embattled Swag said,

if (creditUnits < 12){
    System.out.print("Enter a number greater than 11: ");
    in.next();
}

the in.next() there is tossing away what you enter in. Then the scanner blocks if you input whitespace only until it gets a double here:

creditUnits = in.nextDouble();

If you type anything else in besides whitespace (I'm assuming you were just hitting enter) then it will print out the "You will be taking..." part if it's a valid double. Otherwise it throws an InputMismatchException.

You're probably better off receiving the input and applying your validity checks manually, as opposed to looping nextDouble

public static void main(String[] args) throws IOException {
    System.out.print("Enter the credits you will take each term: ");

    Scanner in = new Scanner(System.in);
    String input = in.next();

    double credits = 0.0;

    while (true) {
        try {
            credits = Double.parseDouble(input);
            if (credits < 12.0) {
                throw new IllegalArgumentException("Must take at least 12 credits.");
            } else {
                break;
            }
        } catch(IllegalArgumentException e) {
            System.out.print("Enter a number greater than 11: ");
            input = in.next();
        }
    }

    System.out.println("You will be taking " + credits + " credits per term.");
    in.close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.