-1

Editing previous post with better info:

APP summary: 2 sections Section 1 has two textnumber inputs + 1 calculation button + 1 textview result Section 2 has two textnumber inputs + 1 calculation button + 1 textview result

So, upon launch everything is fine, no crash.

Now, if all 4 textnumber inputs are not empty, if you press button 1 it will calculate result and put it in result 1, but will reset result 2 to 0. If I press on the second button, opposite happens. Calculation is done correctly, correclty placed in result 2, but result 1 is reset to 0.

But, if any of the 4 inputs happens to be empty, the APP will just quit. Current modified code is below and also debugging result at moment of crash.

package com.example.salescalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
        implements View.OnClickListener{

    Button Pricebutton, MaxCostbutton;
    EditText num1, num2, num3, num4;
    TextView result1, result2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        num1 = (EditText) findViewById(R.id.costinput);
        num2 = (EditText) findViewById(R.id.margininput);
        num3 = (EditText) findViewById(R.id.tpinput);
        num4 = (EditText) findViewById(R.id.margininput2);
        Pricebutton = (Button) findViewById(R.id.buttoncalcprice);
        MaxCostbutton = (Button) findViewById(R.id.buttoncalculatecost);
        result1 = (TextView) findViewById(R.id.priceresult);
        result2 = (TextView) findViewById(R.id.maxcostresult);

        Pricebutton.setOnClickListener(this);
        MaxCostbutton.setOnClickListener(this);
    }
            public void onClick(View v) {
        float cost1, margin1, custTP, margin2;
        float result1A = 0;
        float result2B = 0;

        cost1=Float.parseFloat(num1.getText().toString());
        margin1=Float.parseFloat(num2.getText().toString());
        custTP=Float.parseFloat(num3.getText().toString());
        margin2=Float.parseFloat(num4.getText().toString());

                if (!TextUtils.isEmpty(num1.getText().toString())) {
                    cost1 = Float.parseFloat(num1.getText().toString());
                }
                if (!TextUtils.isEmpty(num2.getText().toString())) {
                    margin1 = Float.parseFloat(num2.getText().toString());
                }
                if (!TextUtils.isEmpty(num3.getText().toString())) {
                    custTP = Float.parseFloat(num3.getText().toString());
                }
                if (!TextUtils.isEmpty(num4.getText().toString())) {
                    margin2 = Float.parseFloat(num4.getText().toString());
                }

        if(v.getId()==R.id.buttoncalcprice) {
            result1A = cost1 / (1- (margin1/100));
        }

        else if (v.getId()== R.id.buttoncalculatecost) {

            result2B = custTP - ((margin2/100)*custTP);
        }

                result1.setText(result1A + "");
                result2.setText(result2B + "");

            }
}

Crash Log

 --------- beginning of crash
2019-10-08 16:46:48.544 4994-4994/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.salescalculator, PID: 4994
    java.lang.NumberFormatException: empty String
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
        at java.lang.Float.parseFloat(Float.java:459)
        at com.example.salescalculator.MainActivity.onClick(MainActivity.java:44)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2019-10-08 16:46:48.555 1381-1425/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 1473729 , only wrote 1473120  
APR
  • 11
  • 2
  • 2
    *Apk crashes after build* Please share crash log with question – AskNilesh Oct 08 '19 at 07:38
  • the App just quits unexpectedly without doing any calculation upon pressing any of the two buttons – APR Oct 08 '19 at 07:55
  • First you need to check weather your `EditText` value is empty or not then convert into `Float` – AskNilesh Oct 08 '19 at 07:55
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Oct 08 '19 at 08:01
  • A `string` formatted value can't be converted into `float` or any primitive type of numbers, like `Integer`, `double`, `short` etc. So, first, check check if the input value is empty or not from the `EditText` and make the the edittext takes input of only decimal numbers from xml. – A S M Sayem Oct 08 '19 at 09:17
  • error : `java.lang.NumberFormatException: empty String` check where is your string variable – Donald Wu Oct 08 '19 at 09:18

4 Answers4

0

At first it is really recommendable to learn about debugging, you'll save much time by doing that instead of running an app and then trying to figure out what the problem could be manually.

Secondly, I ran your code and it works. You don't make any checks wether the input is empty or not a number what you should do but if you give a correct input the code itself works.

Edit:

Your code with a change of your check might look like this for example:

 public void onClick(View v) {
    float cost1, margin1, custTP, margin2;
    float result1A = 0;
    float result2B = 0;


    if (!num1.getText().toString().isEmpty() && num1 != null){
        cost1 = Float.parseFloat(num1.getText().toString());
    } else {
        cost1 = 0f;
    }

    if (!num2.getText().toString().isEmpty() && num2 != null){
        margin1 = Float.parseFloat(num2.getText().toString());
    } else {
        margin1 = 0f;
    }

    if (!num3.getText().toString().isEmpty() && num3 != null){
        custTP = Float.parseFloat(num3.getText().toString());
    } else {
        custTP = 0f;
    }

    if (!num4.getText().toString().isEmpty() && num4 != null){
        margin2 = Float.parseFloat(num4.getText().toString());
    } else {
        margin2 = 0f;
    }


    if(v.getId()==R.id.buttoncalcprice) {
        result1A = cost1 / (1- (margin1/100));
    }

    else if (v.getId()== R.id.buttoncalculatecost) {

        result2B = custTP - ((margin2/100)*custTP);
    }

    result1.setText(result1A + "");
    result2.setText(result2B + "");
}

Also make sure that in the EditText you declare that you only want a number input through android:inputType like this:

<EditText
    android:id="@+id/costinput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />
jle
  • 686
  • 1
  • 8
  • 24
  • hi jle, updated post and log, maybe can help me check it out and give me any suggestion can think of, thanks tons – APR Oct 08 '19 at 08:59
  • `java.lang.NumberFormatException: empty String` simply tells you that the string is empty and there's no input. You need to check wether the input is empty or not, just like you did in your code now but you need to remove the part above the check – jle Oct 08 '19 at 09:37
  • @APR Great, glad I could help! Don't forget to check it as the answer if your problem is solved. – jle Oct 08 '19 at 10:03
  • i would just sorround with try and catch – coroutineDispatcher Oct 08 '19 at 10:09
0

The error is "java.lang.NumberFormatException: empty String" in your onClick method at Float.parseFloat (your error message contains all these details). It means that one of your EditText has empty text (check 41 line in your activity: at com.example.salescalculator.MainActivity.onClick(MainActivity.java:44). So you need to verify that text is not empty before parsing the Float.

Update: It seems you're trying to parse float before check:

cost1=Float.parseFloat(num1.getText().toString());
margin1=Float.parseFloat(num2.getText().toString());
custTP=Float.parseFloat(num3.getText().toString());
margin2=Float.parseFloat(num4.getText().toString());

if (!TextUtils.isEmpty(num1.getText().toString())) {
    cost1 = Float.parseFloat(num1.getText().toString());
}
if (!TextUtils.isEmpty(num2.getText().toString())) {
    margin1 = Float.parseFloat(num2.getText().toString());
}
if (!TextUtils.isEmpty(num3.getText().toString())) {
    custTP = Float.parseFloat(num3.getText().toString());
}
if (!TextUtils.isEmpty(num4.getText().toString())) {
    margin2 = Float.parseFloat(num4.getText().toString());
}
Kirill
  • 7,580
  • 6
  • 44
  • 95
  • @ARP I'd just remove first 4 lines with `Float.parse` but keep all next with `if (!TextUtils.isEmpty)` – Kirill Oct 08 '19 at 10:01
0

A String formatted value (except it can be converted into numbers) can't be converted into float or any primitive type of numbers, like Integer, double, short etc. So, first, check check if the input value is empty or not from the EditText and make the EditText takes input of only decimal numbers from xml.

Use this code which can solve your problem:

<EditText
     android:id="@+id/editText1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:ems="10"
     android:inputType="numberDecimal" />
jle
  • 686
  • 1
  • 8
  • 24
A S M Sayem
  • 2,010
  • 2
  • 21
  • 28
0

Whenever you are using parse you must surround the area with try/catch. That's because empty strings cannot be parsed into numbers (floats, doubles, ints). Therfore:

try{
   if (!num1.getText().toString().isEmpty() && num1 != null){
        cost1 = Float.parseFloat(num1.getText().toString());
    } else {
        cost1 = 0f;
    }

    if (!num2.getText().toString().isEmpty() && num2 != null){
        margin1 = Float.parseFloat(num2.getText().toString());
    } else {
        margin1 = 0f;
    }

    if (!num3.getText().toString().isEmpty() && num3 != null){
        custTP = Float.parseFloat(num3.getText().toString());
    } else {
        custTP = 0f;
    }

    if (!num4.getText().toString().isEmpty() && num4 != null){
        margin2 = Float.parseFloat(num4.getText().toString());
    } else {
        margin2 = 0f;
    }

} catch(NumberFormatException e){
  e.printStackTrace()
}

And btw, that's a problem even in the debug mode, not only when you release APK.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58