-5

I am new to using android studio and I was trying to make a simple average score calculator to get the hang of things. When I tried parsing my input strings to integers it threw this error

Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at app.testscore.testscorecalculator.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)      
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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) 

I wrote the same code as a normal java program and it works fine. Just looking for some help on why it isn't working in android studio , being that I am new to it.

Here is my code for the calculator:

package app.testscore.testscorecalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import java.util.*;

public class MainActivity extends AppCompatActivity {

EditText txtScore1, txtScore2, txtScore3, txtScore4, txtScore5;
TextView lblAvgDisp;
int Score1, Score2, Score3, Score4, Score5, Average;
String First, Second, Third, Fourth, Fifth;

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

txtScore1 = (EditText) findViewById(R.id.txtScore1);
First = txtScore1.getText().toString();
Score1 = Integer.parseInt(First);
txtScore2 = (EditText) findViewById(R.id.txtScore2);
Second = txtScore2.getText().toString();
Score2 = Integer.parseInt(Second);
txtScore3 = (EditText) findViewById(R.id.txtScore3);
Third = txtScore3.getText().toString();
Score3 = Integer.parseInt(Third);
txtScore4 = (EditText) findViewById(R.id.txtScore4);
Fourth = txtScore4.getText().toString();
Score4 = Integer.parseInt(Fourth);
txtScore5 = (EditText) findViewById(R.id.txtScore5);
Fifth = txtScore5.getText().toString();
Score5 = Integer.parseInt(Fifth);

Average = (Score1 + Score2 + Score3 + Score4 + Score5) / 5;

Button btnCalcAvg = (Button) findViewById(R.id.btnCalcAvg);
btnCalcAvg.setOnClickListener(new View.OnClickListener()
    {
        public void onClick (View v)
        {
            lblAvgDisp.setText(Average);
        }
    });



}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 4
    Maybe you think that `""` (an empty string) should be interpreted as **0**. Which is not the case. – Phantômaxx Oct 13 '17 at 16:11
  • 1
    You are trying to parse the empty string. Print the string before parse to integer and see what's went wrong. – Sharan De Silva Oct 13 '17 at 16:13
  • 1
    Which part of the error message, which says that an empty string is not a valid number, is confusing you? --- Be aware that the shown code is not the code that caused that exception, because exception happened in **line 25** when making a call to `Integer.parseInt()`, and there is no such call in line 25 of the shown code. There is in line 26, but not line 25, so code has been modified. – Andreas Oct 13 '17 at 16:18

4 Answers4

1

You are trying to set integer value to Text view. You cannot do this

lblAvgDisp.setText(Average);

Do this

lblAvgDisp.setText(String.valueOf(Average));
Aj121
  • 2,521
  • 1
  • 11
  • 8
0

You error is

Caused by: java.lang.NumberFormatException: For input string: ""

So we must judge whether String is null or not .

And Score1 = Integer.parseInt(First); is need to judge .

Try this .

if(!TextUtils.isEmpty(First)){
    Score1 = Integer.parseInt(First);
}

if(!TextUtils.isEmpty(Second)){
    Score2 = Integer.parseInt(Second);
}
...
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
-1

"" is not a number hence the Exception. You should either wrap the parsing in a try catch or check the input before parsing it.

Jake Castle
  • 281
  • 4
  • 14
-1

The reason you're getting that error is because you're trying to parse an empty string into an integer.

at app.testscore.testscorecalculator.MainActivity.onCreate(MainActivity.java:25)

First = txtScore1.getText().toString();

txtScore1 doesn't contain any input.

Pavan P
  • 645
  • 8
  • 17