0

i have to make a body mass index calculator in java for an assignment, and if the user enters an alphabetic character when it asks for weight, i want the program to quit and display a message. i need help creating the if statement. here is what i have (which isn't working). i have tried various other inequality symbols with no success. i thought i may have to use a if (between command) but i did not discover a way to make that work with the weight variable.

The error i recieve is as follows Lab4.java:21: error cannot find symbol if (weight <= Z) ^

 public class Lab4
    {
            public static void main (String[] args)
            {
                    Scanner kb = new Scanner (System.in);
                    System.out.println("enter e for english or m for metric");
                    String choice;
                    choice = kb.nextLine();
                    char firstchar;
                    firstchar = choice.charAt(0);
            boolean english;
            english = firstchar == 'e';
            if (english)
            {
                    // prompt the user to input their weight in pounds
                    System.out.println("Enter your weight in pounds");
                    double weight = kb.nextDouble();
                    if (weight <= Z)
                    {
                            System.out.println("letters are not an acceptable weight");
                            System.exit(0);
                    }
                    // prompt the user to input their height in inches
                    System.out.println("Enter your height in inches");
                    double height = kb.nextDouble();
                    if (height == 0.0)
                    {
                            System.out.println("0 is not a valid height");
                            System.exit(0);
                    }
                    // make  bmi to an integer and compute bmi by dividing weight by height^2 * 750
                    int bmi = (int)(weight/(height*height)*703);
                    // have the computer display height, wieght, and bmi
                    System.out.printf("your weight is %8.2f", weight);
                    System.out.println(" pounds");
                    System.out.printf("your height is %8.2f", height);
                    System.out.println(" inches");
                    System.out.println("your BMI is " + bmi);
                    if (bmi <= 24)
                            System.out.println("Normal Bodyweight");
                            else
                            {
                                    if (bmi > 30)
                                            System.out.println("Obese");
                                    else
                                            System.out.println("Overweight");

3 Answers3

1

If weight is the input string. You can use the isAlpha function from: https://stackoverflow.com/a/5238524/1926621

Then do if (isAlpha(weight) ) as the condition for checking if the input is alphabetical or not.

Community
  • 1
  • 1
Mohit
  • 857
  • 1
  • 10
  • 26
  • @OusmaneMahyDiaw: Thanks for letting me know!. Removed code, kept source link instead for reference. – Mohit Apr 13 '17 at 22:28
1

In your code, you are reading weight as double using kb.nextDouble() which will throw InputMismatchException and the program will be terminated even before performing/entering your if check (i.e., weight <= Z).

Also, one more important point is that you can't compare double with char type directly i.e., like weight <= Z

So, read weight as string using scanner.nextLine() as shown below and then check if it does not contain alphabets (using regex) as shown below:

String weight = kb.nextLine();
if(!weight.contains(/^\d*\.?\d*$/)) {
    System.out.println("letters are not an acceptable weight");
    System.exit(0);
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

Considering weight is string -

if(!weight.matches("[-+]?\\d*\\.?\\d+")){
   System.out.println("Ikke akseptert. Quitter");
   System.exit(0);
} 
Deepak Tripathi
  • 600
  • 2
  • 4
  • 22