I use this method to check the values from the scanner. My do-while loop prints an error message and then resets to the beginning of the scanner after an invalid value is detected. But it also resets when I've used good values, without the error message.

I've been googling up a storm, but I can't see what I'm doing wrong. Any help would be greatly appreciated.

public boolean invalidInput() {
    if (!(myHeight > 36 && myHeight < 100 || 
        myWeight > 50 && myWeight < 500 || 
        myAge > 12 && myAge < 100) )
      System.out.println ("An unrealistic value has been entered." + 
                         "\n" + "Please enter your information again." + "\n");
    return true;
do{
        
      System.out.print("Enter height in inches: ");
      height = in.nextDouble(); 
      bmi.setHeight(height);      
      
      System.out.print("Enter weight in pounds: ");
      weight = in.nextDouble(); 
      bmi.setWeight(weight);
      
      System.out.print("Enter age in years:     ");
      age = in.nextInt(); 
      bmi.setAge(age);
      
      } while (bmi.invalidInput() == true);

If there is valid entry what do u do ?

It's supposed to print out a table of BMI values. Here, I'll show you everything. Oh, I rewrote the method to make it more readable. But it still behaves the same way.

public class BodyMassIndex {
  public static double myHeight;
  public static double myWeight;
  public static int myAge;
  public static double myBMI;  
  
  // the constructor has two parameters: the height and the weight
  public BodyMassIndex(double height, double weight, int age) {
    myHeight = height;
    myWeight = weight;
    myAge = age;
  }
  
  //getAge returns the age
  public int getAge() {
    return myAge;
  }
  
  //getHeight returns the height 
  public double getHeight() {     
    return myHeight;
  }
  
  // getWeight returns the weight 
  public double getWeight() {
    return myWeight;
  }
  
  // getBMI calculates and returns the BMI
  public double getBMI() {   
    myBMI = (703 * myWeight) / (myHeight * myHeight); 
    return myBMI;
  }
  
  // setHeight sets the height to a new value
  public void setHeight(double height) {  
    myHeight = height;
    myBMI = (703 * myWeight) / (myHeight * myHeight);         
  }
  
  // setWeight sets the weight to a new value
  public void setWeight(double weight) {  
    myWeight = weight;
    myBMI = (703 * myWeight) / (myHeight * myHeight);         
  }
  
  // setAge sets the age to a new value
  public void setAge(int age) {  
    myAge = age;
  }
  
  // setBMI sets the BMI to a new value */
  // New values must be assigned to myBMI and myWeight */
  public void setBMI(double bmi) {
    myBMI = bmi;
    myWeight = ((703*myWeight)*(myHeight*myHeight))/myBMI;
  }
  
  // setMeters sets the height to a new value
  public void setMeters(double meters) {  
    myHeight = meters/0.0254;
    myBMI = (703 * myWeight) / (myHeight * myHeight);     
  }
  
  // setKilograms sets the weight to a new value 
  public void setKilograms(double kilograms) {
    myWeight = kilograms/0.45359237;
    myBMI = (703 * myWeight) / (myHeight * myHeight);     
  }
  
  // getClassification returns the classification of BMI 
  public String getClassification() {
    String c = new String();
    if (myBMI < 18.5) 
      c = "Underweight";
    else if (myBMI >= 18.5 && myBMI < 25.0)
      c = "Normal Weight";
    else if (myBMI >= 25.0 && myBMI < 30.0)
      c = "Overweight";
    else if (myBMI >= 30.0)
      c = "Obese";
    else if (myBMI < 0.0) 
      c = "Invalid Input";
    return (c + "\n");
  }
  
  // returns true if height, weight, or age are unrealistic values
  public boolean invalidInput() {
    if((myHeight < 36 || myHeight > 100) && 
       (myWeight < 80 || myWeight > 500) &&
       (myAge < 12 || myAge > 100)) {
      System.out.println ("You have entered an invalid value." + 
                          "Please re-ener your information." + "\n");      
      return true; // 
    }
    else {
      return false;
    }
  }
  
  // toString returns a string describing the height, weight, and BMI
  public String toString() { 
    return("Height in inches = " + getHeight() + "\n" +
           "Weight in pounds = " + getWeight() + "\n" +
           "Body Mass Index = " + getBMI() );
  }
  
  // Print a table of BMI values and weights for this object's height
  public void printBMITable() {
    System.out.println("Table for A Height of " + myHeight);
    System.out.println("BMI   " + "Weight");
    for (int bmi = 15; bmi <= 35; bmi++) {
      double weight = ( (bmi*(myHeight*myHeight) ) / 703);
      setWeight(weight);
      System.out.println(bmi + "   " + Math.round(weight) ) ;
    }
  }
}
public class BodyMassIndexTest { 
  public static void main(String[] args) {
    
    Scanner in = new Scanner(System.in); 
    char again = 'n'; 
    
    
    do {
      
      double height = 66;
      double weight = 130;
      int age = 30;
      
      BodyMassIndex bmi = new BodyMassIndex(height,weight,age);  // 5
      
      while (bmi.invalidInput() == true){
        System.out.print("Enter height in inches: ");
        height = in.nextDouble(); 
        bmi.setHeight(height);      
        
        System.out.print("Enter weight in pounds: ");
        weight = in.nextDouble(); 
        bmi.setWeight(weight);
        
        System.out.print("Enter age in years:     ");
        age = in.nextInt(); 
        bmi.setAge(age);
      }
      
      System.out.println(bmi.toString() ); 
      System.out.println("Age: " + bmi.getAge() ); 
      System.out.println("BMI Classification: " +
                         bmi.getClassification() );            // 6
      
      bmi.printBMITable();                                       // 7
      
      System.out.print("Make another calculation? y/n ");
      String answer = in.next();
      again = answer.charAt(0);
    
    } while(again == 'Y' || again == 'y');
    
  }
}

Ok, I think I understand what is happening.

Below is the output result from the code you posted (by looking at it this is the desired result).

Height in inches = 66.0
Weight in pounds = 130.0
Body Mass Index = 20.980257116620752
Age: 30
BMI Classification: Normal Weight

Table for A Height of 66.0
BMI Weight
15 93
16 99
17 105
18 112
19 118
20 124
21 130
22 136
23 143
24 149
25 155
26 161
27 167
28 173
29 180
30 186
31 192
32 198
33 204
34 211
35 217
Make another calculation? y/n


However when entering invalid data (age of 1) it still returns that the invalid input is false (which I believe should not be happening).
This is caused because you are using the && operator saying that all the values have to be invalid (what happens if there is only 1 invalid and 2 valid). To fix that change the && operator to the ||, which will force it to go into error validation.

This is the output with the age initially set to 1:

You have entered an invalid value.Please re-ener your information.

Enter height in inches: 66
Enter weight in pounds: 130
Enter age in years: 30
Height in inches = 66.0
Weight in pounds = 130.0
Body Mass Index = 20.980257116620752
Age: 30
BMI Classification: Normal Weight

Table for A Height of 66.0
BMI Weight
15 93
16 99
17 105
18 112
19 118
20 124
21 130
22 136
23 143
24 149
25 155
26 161
27 167
28 173
29 180
30 186
31 192
32 198
33 204
34 211
35 217
Make another calculation? y/n

Hope that provides the answer you are looking for.

Cheers

The &&'s work great to catch bad data and printing the error message, but I still have a problem with the loop still continuing to ask for data even after I've entered correct values.

public boolean invalidInput() {
    if (!( (myHeight > 36 && myHeight < 100) && 
          (myWeight > 50 && myWeight < 500) &&
          (myAge > 12 && myAge < 100)) ) 
      System.out.println ("An unrealistic value has been entered." + "\n" + 
                          "Please enter your information again." + "\n");
    return true;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.