0

I want java to ask the user entail he get both weight or height above 0

                    System.out.print("\tEnter your weight in Kilogram: ");
                    double weight = in.nextDouble();

                    System.out.print("\tEnter your height in Centimeter: ");
                    double height = in.nextDouble();

                    if (weight <= 0 || height <= 0) {

                        while (true) {
    System.out.println("Wrong entry for weight or height... try again");
                        }

                    }

                    String clinic = "NUTRITION";

I couldn't do it without causing an infinite loop

this how i want my output:

    System.out.println("Enter the name (first and last): Omar\n"
            + "Enter your national ID number: 9821444\n"
            + "Enter your age: 30\n"
            + "Enter your mobile number (###-###-####): 055-098-1122\n"
            + "Enter your weight in Kilogram: 0\n"
            + "Enter your height in Centimeter: 176\n"
            + "Wrong entry for weight or height... try again\n"
            + "Enter your weight in Kilogram: 77\n"
            + "Enter your height in Centimeter: 0\n"
            + "Wrong entry for weight or height... try again\n"
            + "Enter your weight in Kilogram: 77\n"
            + "Enter your height in Centimeter: -176\n"
            + "Wrong entry for weight or height... try again\n"
            + "Enter your weight in Kilogram: 77\n"
            + "Enter your height in Centimeter: 176");
RaminS
  • 2,208
  • 4
  • 22
  • 30
David
  • 37
  • 7

3 Answers3

0

Try this:

double height = 0;
double weight = 0;

while (weight <= 0 || height <= 0) {

    System.out.print("\tEnter your weight in Kilogram: ");
    weight = in.nextDouble();

    System.out.print("\tEnter your height in Centimeter: ");
    height = in.nextDouble();

   if (weight <= 0 || height <= 0) {
       System.out.println("Wrong entry for weight or height... try again");
   }
}
Blady214
  • 729
  • 6
  • 19
0

Expand your loop to the entirety of your input logic:

            do {
                System.out.print("\tEnter your weight in Kilogram: ");
                double weight = in.nextDouble();

                System.out.print("\tEnter your height in Centimeter: ");
                double height = in.nextDouble();

                if (weight <= 0 || height <= 0) {
                    System.out.println("Wrong entry for weight or height... try again");
                }
            } while(weight <= 0 || height <= 0)
Zircon
  • 4,677
  • 15
  • 32
0

try this:

             boolean cont = true;

            while (cont) {
            System.out.print("\tEnter your weight in Kilogram: ");
            double weight = in.nextDouble();

            System.out.print("\tEnter your height in Centimeter: ");
            double height = in.nextDouble();

            if (weight <= 0 || height <= 0) {


        System.out.println("Wrong entry for weight or height... try again");
                }else{
                    cont=false;
                }
            }
saumik gupta
  • 167
  • 2
  • 10