Search code examples
javaswingjframejtextfieldnumberformatexception

leave a blank jtextfield field in java


I have this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at sunat.calculadora.btnCalcularActionPerformed(calculadora.java:234)
    at sunat.calculadora.access$100(calculadora.java:14)
    at sunat.calculadora$2.actionPerformed(calculadora.java:109)
    at 

My code:

private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {                                            
 int sueldo,gratificaciones;
 int renumeracion_bruta_anual;
 int horas_extras,meses_pendientes;
 int renumeracion_neta_anual;
 double  impuesto_anual_proyectado;
 int asignacion_familiar;

 horas_extras=0;

 sueldo=Integer.parseInt(txtsueldo.getText());
 horas_extras = Integer.parseInt(txthorasextras.getText());



 meses_pendientes=Integer.parseInt(txtmesespendientes.getText());
 gratificaciones=Integer.parseInt(txtgratificacion.getText());
 asignacion_familiar=Integer.parseInt(txtasignacionfamiliar.getText());



 renumeracion_bruta_anual=sueldo*meses_pendientes+gratificaciones+horas_extras+asignacion_familiar;
 renumeracion_neta_anual=renumeracion_bruta_anual-29400;





 if (renumeracion_neta_anual<1){
       JOptionPane.showMessageDialog (null, "Usted no paga impuesto");
       }

 if (renumeracion_neta_anual <= 21000 && renumeracion_neta_anual>=1) {

 impuesto_anual_proyectado=renumeracion_neta_anual*0.08;
 txtcalculo.setText(String.format("%.2f", impuesto_anual_proyectado));

 } else if (renumeracion_neta_anual > 21000 && renumeracion_neta_anual<=84000) {
 impuesto_anual_proyectado=renumeracion_neta_anual *0.14;
  txtcalculo.setText(String.format("%.2f", impuesto_anual_proyectado));
 } else if (renumeracion_neta_anual > 84000 && renumeracion_neta_anual<=147000) {
 impuesto_anual_proyectado=renumeracion_neta_anual *0.17;
 txtcalculo.setText(String.format("%.2f", impuesto_anual_proyectado));
 } else if (renumeracion_neta_anual >147000 && renumeracion_neta_anual<=18900) {

 impuesto_anual_proyectado=renumeracion_neta_anual *0.20;
 txtcalculo.setText(String.format("%.2f", impuesto_anual_proyectado));
 } else if (renumeracion_neta_anual >18900) {
 impuesto_anual_proyectado=renumeracion_neta_anual *0.30;
 txtcalculo.setText(String.format("%.2f", impuesto_anual_proyectado));



 }         

} 

As you can see the field hours_extras is declared as an integer but at the moment leaving the field hours_extras empty it becomes String and does not perform the mathematical operation giving the error above. There is some way to let go without writing anything in the jtexfield since I must leave that field empty and calculate the final amount without it when it is not required. Thank you for your answers.


Solution

  • You can do something like the following if you have to handle empty strings as a special case, and would like gibberish inputs to still throw an Exception:

    String extraHourString = txthorasextras.getText();
    if(extraHourString.isEmpty()) { //alternatively, if(extraHourString.equals("")) {
        ... //handle the text being empty, or set a boolean flag so that you can handle it later
    }
    else { // we know the provided text is not empty now
        //will still crash on gibberish inputs, but the empty string "" will never reach here
        horas_extras = Integer.parseInt(extraHourString);
    }
    

    Alternatively, if you would like to completely ignore the empty string (leaving horas_extras to a default value), you can do something like the following:

    int horas_extras = DEFAULT_VALUE; //declaration sets the default (could be any integer)
    ...
    if(!txthorasextras.getText().equals("")) { //or, if(!txthorasextras.getText().isEmpty()) {
        horas_extras = Integer.parseInt(txthorasextras.getText());
    }
    

    This will ignore the assignment to horas_extras if the input string is empty, which will leave it at the current value of horas_extras (DEFAULT_VALUE). Note that gibberish inputs still throw an Exception as before.