user3008653
user3008653

Reputation: 13

I am using Java and my while statement isn't working

If you do enter a value that isn't correct it prompts you to enter the right amount but after entering the right or wrong amount it simply ends the program. Sorry I should have mentioned earlier that the program goes on for 12 months. My problem is that once I've been prompted to enter the right amount for any given month after I've entered the wrong amount, the program takes my answer and ends, rather than taking the correct answer and moving on to the next month.

    System.out.print("Please enter monthly kWh usage for month 1: ");
    month1 = scan.nextInt();
    do {
          System.out.print(" Please enter a valid amount: ");
          month1 = scan.nextInt();
    } while (month1>10000 || month1<0);

    System.out.print("Please enter monthly kWh usage for month 2: ");
    month2 = scan.nextInt();
    do {
        System.out.print(" Please enter a valid amount: ");
        month2 = scan.nextInt(); 
    } while (month2>10000 || month2<0);   

Upvotes: 0

Views: 143

Answers (4)

Paul Samsotha
Paul Samsotha

Reputation: 209052

Maybe something like this is what you want. The do is what's messing you up, because if the first input meets your condition, the loops will still occur at least once. Just use a while loop

System.out.print("Please enter monthly kWh usage for month 1: ");
month1 = scan.nextInt();

while (month1 > 10000 || month1 < 0) {
    System.out.print(" Please enter a valid amount: ");
    month1 = scan.nextInt();
}

System.out.print("Please enter monthly kWh usage for month 2: ");
month2 = scan.nextInt();

while (month2 > 10000 || month2 < 0) {
    System.out.print(" Please enter a valid amount: ");
    month2 = scan.nextInt();
}

Edit: Running code

import java.util.Scanner;

public class Month {

public static void main(String[] args) {
    int month1;
    int month2;
    Scanner scan = new Scanner(System.in);
    System.out.print("Please enter monthly kWh usage for month 1: ");
month1 = scan.nextInt();

while (month1 > 10000 || month1 < 0) {
    System.out.print(" Please enter a valid amount: ");
    month1 = scan.nextInt();
}

System.out.print("Please enter monthly kWh usage for month 2: ");
month2 = scan.nextInt();

while (month2 > 10000 || month2 < 0) {
    System.out.print(" Please enter a valid amount: ");
    month2 = scan.nextInt();
}
}
}

Upvotes: 2

Satheesh Cheveri
Satheesh Cheveri

Reputation: 3679

First of all, when do you use do..while, you would not need to capture input outside the loop. This force you to enter the input twice, you can remove redundant statement.

Secondly, do you have any statement to executed after second do..while?

I suggest you to provide more details what do you want to achieve here?

Upvotes: 0

aksh1t
aksh1t

Reputation: 5448

Your code is totally correct to "simply end the program" because there are no other lines of code to execute!

The first do while loop will take an input and store it in the variable month1 and then check if it is between 0 and 10000. If so, it will go out of the loop, otherwise it will run the loop again and again till you enter the value between 0 and 10000. Then the second loop will do the same with month2 variable.

After storing both the variables, the program will exit without doing anything, because there are no lines of code to execute. You will need to write some lines of code after this to do something.


P.S. Also, it is good practice to surround the nextInt() with try-catch block because entering a non-numeric value will cause the program to crash.


Edit:

Also, you need to remove the month1 = scan.nextInt(); and month2 = scan.nextInt(); lines because the do-while loop takes care of initialising the variables for the first time.

Upvotes: 3

Kieveli
Kieveli

Reputation: 11084

nextint() will throw an exception if the next characters aren't ints:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range 

Try using an exception block:

try {
  ...
}
catch( InputMismatchException e ) {
    System.out.println( e.getMessage() );
    e.printStackTrace();
}

Upvotes: 0

Related Questions