4

I have been doing a project for my java class. For the project I have to have the user enter input and calculate their body mass index and body surface area the program is supposed to remain running until the user enters a "q". I cannot get my program to stop running when a "q" is put in it just crashes. Also I am very new to java and programming in general so I would appreciate any help. My code is as follows. Thanks : )

public static void main(String[] args) {

        //Scanner
        Scanner stdIn = new Scanner(System.in);

        //Variables
        final double METERS_TO_CM = 100;   // The constant to convert meters to centimeters
        final double BSA_CONSTANT = 3600;  // The constant to divide by for bsa
        double bmi;                        // Body Mass Index
        String weight;                     // Weight in kilograms
        String height;                     // Height in meters
        String classification;             // Classifies the user into BMI categories 
        double bsa;                        // Body surface area



        do {
            System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
            weight = stdIn.next();
            System.out.print("Enter height in meters: ");
            height = stdIn.next();
            double height2 = Double.parseDouble(height);
            double weight2 = Double.parseDouble(weight);
            bmi = weight2/(height2*height2);
        if (bmi < 18.5)
        {
            classification = "Underweight";
        }
        else if (bmi < 25)
        {
            classification = "Normal";
        }
        else if (bmi < 30)
        {
            classification = "Overweight";
        }
        else
        {
            classification = "Obese";
        }

        System.out.println("Your classification is: " + classification);
        bsa = Math.sqrt(((height2*METERS_TO_CM)*weight2)/BSA_CONSTANT);
        System.out.printf("BMI: %.1f\n", bmi);
        System.out.printf("BSA: %.2f\n", bsa);


        System.out.println("Hit 'q' to quit");
        } while (stdIn.nextLine().compareToIgnoreCase("q")==0);






    }
}
alestanis
  • 21,519
  • 4
  • 48
  • 67
Brad
  • 49
  • 2
  • 7
  • One of these days, someone's going to have a question about calculating BMI and it's not going to be homework. :-) – corsiKa Feb 24 '11 at 23:17
  • Yeah well for now I'm just worried about the homework : ) – Brad Feb 24 '11 at 23:19
  • Oh it wasn't anything against you, or the question. This is one of those "classic" homework assignments that everyone does in one class or another. I just know one of these days, some insurance guy who doesn't know anything about programming is going to be trying to make a BMI calculator for his insurance reports and people will think it's homework. Hehehe. – corsiKa Feb 24 '11 at 23:26
  • @Brad Do not forget to accept the responses if they are correct ;) – Serhiy Feb 24 '11 at 23:32
  • @glowcoder Yeah I know I didn't take offense I was just making a little joke : ) – Brad Feb 24 '11 at 23:35
  • possible duplicate of [ending a do-while loop](http://stackoverflow.com/questions/5131521/ending-a-do-while-loop) – andersoj Feb 27 '11 at 20:34

5 Answers5

1

I would guess that your "q" input is written in weight and therefore you try to parse it to a Double, which throws an unhandled Exception and stops the execution.

You should handle this Exception and make the system break the while loop when triggering it.

Dunaril
  • 2,757
  • 5
  • 31
  • 53
  • I compiled his code - he doesn't get an exception. Good thought though. – corsiKa Feb 24 '11 at 23:22
  • @glowcoder Actually I am not sure I understood - the program should stop running but instead it crashes without an exception ? What is the difference ? – Dunaril Feb 24 '11 at 23:24
  • Well, it doesn't "crash". Compile his code and it makes sense. It's not querying the user for the input at the end of the loop. – corsiKa Feb 24 '11 at 23:25
1

You're grabbing the entire line for your while loop condition.

Try just grabbing the next() instead of nextLine().

Also, you're looking at while it DOES equal 0 ... meaning equal. I'd change that to != instead. You want to continue looping while the next token is NOT Q.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • Yes! that worked thank you I've been sitting here looking at this for the last 2 hours trying to figure this out. I appreciate it – Brad Feb 24 '11 at 23:27
  • 1
    I know it works because I compiled it into your code on my machine. :-) Now, these are answers to the question you asked... BUT... Dunaril, while he didn't answer this question, did bring up a very important point. What happens when you put "abc" for your weight? It dies... You can actually recover from that! I don't know if you've learned about exception handling, but if you haven't you should look into it. – corsiKa Feb 24 '11 at 23:29
  • Yeah I know I was going to try and do that part after I got this working. – Brad Feb 24 '11 at 23:33
0

Don't actually use a loop. Since it's impractical someone would ever max the call stack out by answering too many questions, just make the whole operation a function. At the end of the function, call itself if the result isn't Q.

Oliver Kane
  • 888
  • 1
  • 6
  • 23
0

its simple use this

while (true)
{
    Console.WriteLine("Start processing");
    //processing code here
    Console.WriteLine("finish processing");

    Console.WriteLine("start again? y/n?");
    if (Console.ReadLine().Equals("n"))
        break;
}
Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
0

Let's make a structural change to make this easier for you to do.

We are going to change it so that your do-while loop always is running, until you explicitly tell it to stop.

Your current while is:

while(stdIn.nextLine().compareToIgnoreCase("q")==0);

Which works ok, but we have a more simple way to do this. Have you heard of the break statement?

I would suggest you use break. This statement will 'break' you out of the while loop; basically it tells the program to stop looping when it is called. This will be a bit easier to follow than your somewhat confusing do-while.

do {

   //Your Do code goes here, as before
   ...
   //

   //Your newly added break statement will go here. 
   //This breaks out of the while loop when your inputed 'choice' value is
   //equal to the string of "q" (for quit)
   if (Choice.equals("q"))){
     break;
     //When break is called nothing else in the loop will run
   }

   //Same thing but with the string of "quit"
   if (Choice.equals("quit"){
     break;
   }



}while (true); 
//Your new while statement is simply while(true) which will run until break is called

Hopefully that is helpful to you.

Panky
  • 563
  • 1
  • 3
  • 18