ssj3goku878
ssj3goku878

Reputation: 755

Java JTextField empty string error

I keep getting an error for empty string as you can see below

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

I am unsure why I keep getting this error since I do not try to calculate without typing a number within the JTextField. Below is the code I am using for the calculate button which is fairly simple.

private void ButtonCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                                
         Height = Integer.parseInt(this.TextFieldHeight.getText());
         Weight = Integer.parseInt(this.TextFieldWeight.getText());
         BMI = Integer.parseInt(this.TextFieldBMI.getText());
         answer = Height + Weight;
        this.TextFieldBMI.setText("The answer is: " + answer);
    }                                               

These are the defined variables

//variables
    static int Height;
    static int Weight;
    static int BMI;
    static int answer;

Upvotes: 1

Views: 4166

Answers (4)

mussdroid
mussdroid

Reputation: 732

I believe you keep TextFieldBMI empty when you press the jButton which you want to print the answer in! You should comment the line in the code shown below. If you want enter new BMI value you need to create another textfield for it and for the answer create another textfield as well . If you keep the TextFieldBMI empty with your original code , and press the button you will get the same error again.

enter image description here

Commented Your Code and it is working ;

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

         Height = Integer.parseInt(this.TextFieldHeight.getText());
         Weight = Integer.parseInt(this.TextFieldWeight.getText());
//       BMI = Integer.parseInt(this.TextFieldBMI.getText());
         answer = Height + Weight;
        this.TextFieldBMI.setText("The answer is: " + answer);



    }   

If you want to calculate BMI ;

enter image description here

Your Fields Should be ;

    static int Height;
    static int Weight;
    static double BMI;
    static int answer;

Your Button Action Should be;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        Height = Integer.parseInt(this.TextFieldHeight.getText());
        Weight = Integer.parseInt(this.TextFieldWeight.getText());
        BMI = getBMI(Weight, Height);
        this.TextFieldBMI.setText("BMI is  " + BMI);


    } 

BMI Calculation Method Should Be ;

private static double getBMI(int weightInKg, int heightInCm) { 

    return weightInKg / Math.pow(((double) heightInCm / 100), 2); 
} 

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 22972

You are clicking on button while one of your textfields is empty "" you should add empty check before going for parsing.

     if(!TextFieldHeight.getText().trim().equals("") && 
        !TextFieldWeight.getText().trim().equals("") && 
        !TextFieldBMI.getText().trim().equals("")){
      //Parse string
     }

Note: As per naming convention variable names should start with small case.

You may also face the exception when you enter some String which can not be parsed to integer.

Upvotes: 1

ssj3goku878
ssj3goku878

Reputation: 755

Sorry I apologize! I added the BMI variable when I shouldn't have since an answer is suppose to be outputted into the TextFieldBMI rather than .getText() from it so it was always empty. Problem solved!

Upvotes: 1

Gumbo
Gumbo

Reputation: 1746

This happens when the TextField is empty (getText() returns ""), so the Integer class can't parse anything and throws an exception instead. You should sourround the parsing code with try/catch:

try{
    Height = Integer.parseInt(this.TextFieldHeight.getText());
    Weight = Integer.parseInt(this.TextFieldWeight.getText());
    BMI = Integer.parseInt(this.TextFieldBMI.getText());
    answer = Height + Weight;
    this.TextFieldBMI.setText("The answer is: " + answer);
catch(NumberFormatEeceptinon e){
    //So something when paring failed, e.g. show a error message
}

Upvotes: 1

Related Questions