views:

94

answers:

5

This is my BMI calculator with GUI. It compiles, but if the user enters characters rather than numbers into the fields, the program crashes. What is wrong with my catching of the numberformatexception? Id appreciate any help!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Exercise1b extends JFrame{
 JLabel BMIlabel = new JLabel("Your BMI is ?");
 JTextField height = new JTextField(10);
 JTextField weight = new JTextField(10);

 Exercise1b(){
  super("BMI-calculator");
  setLayout(new FlowLayout());

  add(new JLabel("Enter your height in centimeters: "));
  add(height);

  add(new JLabel("Enter your weight in kilograms: "));
  add(weight);

  JButton button = new JButton("Calculate");
  button.addActionListener(new CalculateListener());
  add(button);

  add(BMIlabel);

  setSize(500, 500);
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  }



 class CalculateListener implements ActionListener{
   public void actionPerformed(ActionEvent ave){
    try {
     double gotheight = Double.parseDouble(height.getText());
     double gotweight = Double.parseDouble(weight.getText());
     double userBMI = ((gotweight/(gotheight*gotheight))*10000);
     BMIlabel.setText("Your BMI is " + (int)userBMI + " .");
     }
    catch (NumberFormatException exc) {
     System.out.println("Not valid number");
     }

    //double gotheight = Double.parseDouble(height.getText());
    //double gotweight = Double.parseDouble(weight.getText());

    //double userBMI = ((gotweight/(gotheight*gotheight))*10000);

    //BMIlabel.setText("Your BMI is " + (int)userBMI + " .");
    } 
   }


 public static void main(String[] args){

  new Exercise1();
  }


 }
+1  A: 

I suggest you to see Validating Numerical Input in a JTextField and Working with Fonts, also check the NumberFormatException at Java Docs to learn more about this and understand what you're going to do, because as I can see your program is working fine, this thing that you're saying about the "crash" is the exception. ;)

Nathan Campos
+6  A: 

I had to make 1 change to your code to get it to compile. In your main() method I changed:

new Exercise1();

to

new Exercise1b();

I then ran some numeric test cases and the program appeared to work as designed. When I entered non-numeric data, it did not crash. It simply outputted a message to the console indicating that non-numeric data was entered:

Not valid number

which appears to be what the code was trying to do. I see no problem with this code.

Asaph
I appreciate you trying this out yourself. The posted question looked suspiciously like it wasn't the code actually being executed...
Adam Paynter
@Adam Paynter: I agree. Perhaps the compile step isn't happening. Or maybe he's got a different executable running than the one he's producing with the compiler.
Asaph
+3  A: 

I just tried it and it works fine on my machine, except that new Exercise1() should be new Exercise1b().

Check your build process and make sure that you are actually running the code you are editing. This is a common mistake even experienced programmers make.

Otto Allmendinger
A: 

That program doesn't even compile ... once you spell the name of your class correctly in main, it works as intended.

meriton
A: 

OMG Fail the main method tries to create a new Exercise1, but it should be creating an Exercise 1b. Gee, sorry folks, my bad. Thank you for the advice about how to format posts though.

Fred