java.lang.NumberFormatException: Invalid double: ""

1

I am new to this android studio and I try to make a temperature converter the problem is that no matter how hard I try to get this error it is assumed that the whole program should do object oriented

  

01-29 07: 44: 26.967 12711-12711 / com.example.brolix.ctemperature E / AndroidRuntime: FATAL EXCEPTION: main                                                                                    Process: com.example.brolix.ctemperature, PID: 12711                                                                                    java.lang.NumberFormatException: Invalid double: ""                                                                                        at java.lang.StringToReal.invalidReal (StringToReal.java:63)                                                                                        at java.lang.StringToReal.parseDouble (StringToReal.java:267)                                                                                        at java.lang.Double.parseDouble (Double.java301)                                                                                        at com.example.brolix.ctemperatura.MainActivity $ 3.onEditorAction (MainActivity.java:66)                                                                                        at android.widget.TextView.onEditorAction (TextView.java:4794)                                                                                        at com.android.internal.widget.EditableInputConnection.performEditorAction (EditableInputConnection.java:139)                                                                                        at com.android.internal.view.IInputConnectionWrapper.executeMessage (IInputConnectionWrapper.java:304)                                                                                        at com.android.internal.view.IInputConnectionWrapper $ MyHandler.handleMessage (IInputConnectionWrapper.java:78)                                                                                        at android.os.Handler.dispatchMessage (Handler.java:102)                                                                                        at android.os.Looper.loop (Looper.java:135)                                                                                        at android.app.ActivityThread.main (ActivityThread.java:5910)                                                                                        at java.lang.reflect.Method.invoke (Native Method)                                                                                        at java.lang.reflect.Method.invoke (Method.java:372)                                                                                        at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1405)                                                                                        at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1200)

This is my main activity, I understand that my problem is the data conversion of my method (double)

 EditText C, F, K;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    C = findViewById(R.id.celciusnumero);
    F = findViewById(R.id.farennumero);
    K = findViewById(R.id.kelvinumero);

    final celcius c = new celcius();
    c.setLetra('C');
    final fahrenheit f = new fahrenheit();
    f.setLetra('F');
    final kelvin k = new kelvin();
    k.setLetra('K');

    try {
        C.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                k.setNumero(Double.parseDouble(K.getText().toString()));
                f.setNumero(Double.parseDouble(F.getText().toString()));
                c.conversion(f);

                F.setText(f.getNumero().toString());

                c.conversion(k);

                K.setText(k.getNumero().toString());
                return false;
            }
        });
        F.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                k.setNumero(Double.parseDouble(K.getText().toString()));
                c.setNumero(Double.parseDouble(C.getText().toString()));

                f.conversion(c);

                C.setText(c.getNumero().toString());

                f.conversion(k);

                K.setText(k.getNumero().toString());

                return false;
            }
        });
        K.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                f.setNumero(Double.parseDouble(F.getText().toString()));
                c.setNumero(Double.parseDouble(C.getText().toString()));

                k.conversion(c);

                C.setText(c.getNumero().toString());

                k.conversion(f);

                F.setText(f.getNumero().toString());

                return false;
            }
        });
    } catch (NumberFormatException e) {

    }
}
    
asked by Diego Pineda Cervantes 29.01.2018 в 14:54
source

2 answers

0

The data that you are transforming is "" therefore the program will fail, it is very important that when you obtain data directly from the screen, you make validations.

For example:

1) Validate if the data is null or is returned empty.   
if(K.getText().toString().isEmpty){ //<-- Esto devuelve un booleano // Si esta vacío }  
2) Check that the data is a double    
  - You can use the classic try catch

 try {
    //Aqui la validacion
}
catch (NumberFormatException e) {
    // Si entra aqui significa que el dato no es un número.
}


  3) Also a regular expression:     
String decimalPattern = "([0-9]*)\.([0-9]*)";
String number="20.00";
boolean match = Pattern.matches(decimalPattern, number); System.out.println(match); //if true then decimal else not

    
answered by 29.01.2018 / 16:30
source
2

The error is described here:

  

NumberFormatException: Invalid double: "" at   java.lang.StringToReal.invalidReal (StringToReal.java:63) at   java.lang.StringToReal.parseDouble (StringToReal.java:267) at   java.lang.Double.parseDouble (Double.java301) at

you are trying to convert double to% co_of% empty, which is incorrect.

I advise you to carry out the validation to determine if the value is a String empty (in this case you can assign a value of 0 as a default) or it contains a numeric value, if it contains a numerical value it performs the conversion

In Android you can use the String method of the class isEmpty() to determine if the TextUtils is null or zero length:

String valorK = K.getText().toString()
if (TextUtils.isEmpty(valorK)) {
    valorK = "0"; //Asigna valor default
}    
k.setNumero(Double.parseDouble(valorK));

String valorF = F.getText().toString()
if (TextUtils.isEmpty(valorF)) {
    valorF = "0"; //Asigna valor default
}
f.setNumero(Double.parseDouble(valorF));

With the above we solve the case in which the String is empty, but the best option would be to validate the case in which the value is not numeric , for this you can use this method:

public static boolean esNumerico(String number){
    boolean result = false;
    try{
        if(number != null){
            Double.parseDouble(number);
            result = true;
        }
    }catch(NumberFormatException nfe){
        nfe.printStackTrace();
    }
    return result;
}

and apply it in this way, establishing in the case of not being numeric a default value of 0:

  k.setNumero(Double.parseDouble(esNumerico(K.getText().toString())?K.getText().toString():"0"));
  f.setNumero(Double.parseDouble(esNumerico(F.getText().toString())?F.getText().toString():"0"));

Another variation of the method String which returns a default value (0) if it is not numeric:

public static double esNumerico(String number){
    double result = 0;
    try{
        if(number != null){
            result = Double.parseDouble(number);
        }
    }catch(NumberFormatException nfe){
        nfe.printStackTrace();
    }
    return result;
}

It would reduce the code and would be applied in this way:

 k.setNumero(esNumerico(K.getText().toString()));
 f.setNumero(esNumerico(F.getText().toString()));
    
answered by 29.01.2018 в 16:33