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);
}
});
}