Giogiosw
Giogiosw

Reputation: 11

Caused by: java.lang.NumberFormatException: Invalid int: "9.9"

I have a problem with entering numbers with a comma (.), Thanks to your help last time I solved another problem, I hope even today. the problem is this, I have a decimal numeric input field, but just inserico a number with a comma application crashes

someone knows how to help me? The code below is java and xml

Code java.

        EditText x_tensione = (EditText)findViewById(R.id.Tensione);
        EditText x_potenza = (EditText)findViewById(R.id.Potenza);
        EditText x_lunghezza = (EditText)findViewById(R.id.Lunghezza);
        EditText x_caduta_tensione = (EditText)findViewById(R.id.Caduta_tensione);

            String xtensione = x_tensione.getText().toString();
            String xpotenza = x_potenza.getText().toString();
            String xlunghezza = x_lunghezza.getText().toString();
            String xcaduta_tensione = x_caduta_tensione.getText().toString();

            double NumeroTensione = Integer.parseInt(xtensione);
            double NumeroPotenza = Integer.parseInt(xpotenza);
            double NumeroLunghezza= Integer.parseInt(xlunghezza);
            double NumeroCaduta = Integer.parseInt(xcaduta_tensione);

            double molt = 2* NumeroPotenza;
            double resistenza = (NumeroCaduta / molt) * NumeroTensione;
            double xx = 0.0178;
            double x3 = xx * (NumeroLunghezza / resistenza);
            double x = x3;
            TextView Tview = (TextView)findViewById(R.id.uscita);
            Tview.setText(String.valueOf(x));

code xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:text="Seleziona corrente"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            />

        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="102dp"
            android:orientation="vertical">
            <RadioButton android:id="@+id/continua"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Continua"
                android:onClick="onRadioButtonClicked"
                />
            <RadioButton android:id="@+id/monofase"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Monofase alternata"
                android:onClick="onRadioButtonClicked"
                />
            <RadioButton android:id="@+id/trifase"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Trifase alternata"
                android:onClick="onRadioButtonClicked"
                />
        </RadioGroup>

</LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Tensione"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Tensione"
            android:layout_weight="1"
            android:numeric="decimal" />

    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Potenza"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Potenza"
            android:layout_weight="1"
            android:numeric="decimal" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Caduta tensione"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:numeric="decimal" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText5"
            android:layout_weight="1"
            android:numeric="decimal" />
    </LinearLayout><LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="Lunghezza"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
       />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText6"
        android:layout_weight="1"
        android:numeric="decimal" />

</LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="Risultato ="
            android:layout_width="130dp"
            android:layout_height="wrap_content"
             />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/uscita" />

    </LinearLayout>

    <Button
        android:id="@+id/backbutton"
        android:background="#9941e7d2"
        android:radius="4dp"
        android:shape="rectangle"
        android:text="Calcola "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="Mostra_Risultato" />

</LinearLayout>

Upvotes: 0

Views: 1791

Answers (2)

Ryan J
Ryan J

Reputation: 8323

Use Double.parseDouble to get double values from a String.

You should probably also wrap those sections in try/catch blocks and specifically handle the NumberFormatException that you are getting.

Upvotes: 0

Tim B
Tim B

Reputation: 41208

You have a floating point number "9.9" that you are trying to convert to an integer, which cannot contain that value.

You need to map it to a floating point number (Float or Double) or trim the ".9" before converting.

Upvotes: 5

Related Questions