Check double - Java

0

I am not able to do this check already tried everything, but it always gives me this error:

  

java.lang.NumberFormatException: Invalid double: ""

If anyone can help, thank you.

package com.calculadora;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    private Toolbar toolbar;
    private Button btncalc, btnlimpar;
    private AppCompatEditText edn1, edn2;
    private AppCompatTextView txtResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        btncalc = (Button) findViewById(R.id.calcular);
        btnlimpar = (Button) findViewById(R.id.limpar);

        edn1 = (AppCompatEditText)findViewById(R.id.n1);
        edn2 = (AppCompatEditText)findViewById(R.id.n2);

        txtResult = (AppCompatTextView)findViewById(R.id.result);

        btncalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                double n1 = Double.parseDouble(edn1.getText().toString());
                double n2 = Double.parseDouble(edn2.getText().toString());
                double res = (n1*2 + n2*3) / 5;

                if(res >= 6){
                    txtResult.setText("Voce Foi Aprovado Parabens Sua nota é: " + res);
                    txtResult.setTextColor(Color.parseColor("#3F51B5"));
                    edn1.setText("");
                    edn2.setText("");

                }else if(res < 6){
                    txtResult.setText("Voce Foi Reprovado Estude Mais da Próxima Sua nota é: " + res);
                    txtResult.setTextColor(Color.parseColor("#F44336"));
                    edn1.setText("");
                    edn2.setText("");

 /*Linha Com Erro é essa*/                   }else if(TextUtils.isEmpty(edn1.getText().toString()) && TextUtils.isEmpty(edn2.getText().toString())) {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                        dialog.setTitle("Aviso");
                        dialog.setMessage("Campo Vazio!");
                        dialog.setNeutralButton("OK", null);
                        dialog.show();
                    }

                }
            });

        btnlimpar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edn1.setText("");
                edn2.setText("");
                txtResult.setText("");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.action_sobre){
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Sobre o App");
            dialog.setMessage(R.string.mensage);
            dialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent it = new Intent();
                    it.setData(Uri.parse("http://www.facebook.com/diogosilvam"));
                    startActivity(it);
                }
            });
            dialog.show();

        }

        return super.onOptionsItemSelected(item);
    }
}

XML:

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />


<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    android:padding="10dp">

    <android.support.v7.widget.CardView

        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_marginTop="50dp"
        app:cardBackgroundColor="#fff"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/logo" />

        <android.support.v7.widget.AppCompatTextView
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:paddingLeft="48dp"
            android:text="Preencha as suas notas para calcular sua média"
            android:textColor="#000"
            android:textSize="18sp" />

        <android.support.v7.widget.AppCompatTextView
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:paddingLeft="14dp"
            android:text="N1 (0-10)"
            android:textColor="#000"
            android:textSize="18sp" />

        <android.support.v7.widget.AppCompatEditText
            android:textStyle="bold"
            android:id="@+id/n1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="90dp"
            android:hint="0.0"
            android:numeric="decimal"
            android:paddingLeft="14dp"
            android:textColor="#000"
            android:textColorHint="#919191"
            android:maxLength="3"/>

        <android.support.v7.widget.AppCompatTextView
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="130dp"
            android:paddingLeft="14dp"
            android:text="N2 (0-10)"
            android:textColor="#000"
            android:textSize="18sp" />

        <android.support.v7.widget.AppCompatEditText
            android:textStyle="bold"
            android:maxLength="3"
            android:isScrollContainer="false"
            android:id="@+id/n2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="160dp"
            android:hint="0.0"
            android:numeric="decimal"
            android:paddingLeft="14dp"
            android:textColor="#000"
            android:windowSoftInputMode="adjustPan"
            android:textColorHint="#919191" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <Button
                android:textStyle="bold"
                android:id="@+id/calcular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="210dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                android:stateListAnimator="@anim/btn_anim"
                android:text="Calcular" />

            <Button
                android:textStyle="bold"
                android:id="@+id/limpar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="12dp"
                android:layout_marginTop="210dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                android:stateListAnimator="@anim/btn_anim"
                android:text="Limpar" />
        </LinearLayout>

        <android.support.v7.widget.AppCompatTextView

            android:textStyle="bold"
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="270dp"
            android:paddingLeft="14dp"
            android:text="Resultado"
            android:textColor="#000"
            android:textSize="18sp" />
    </android.support.v7.widget.CardView>
</LinearLayout>

    
asked by anonymous 24.03.2016 / 18:42

1 answer

1

You can use the function before doing Double.parseDouble to check if the String can be converted to Double.

boolean isDouble(String str) {
    try {
        Double.parseDouble(str);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
    
24.03.2016 / 21:42