Issue
I want to make app that solve Two variable two equation like;
aX+bY=c---(1)
dX+eY=F---(2)
And in My app i made six edit texts a,b,c,d,e,f. And other Text view to show X+,Y+ ,=. and then what values i fetch i set into Text view of valX and valY
package com.shubh.college_calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.shubh.college_calculator.databinding.ActivityTwoVarBinding;
public class Two_var_act extends AppCompatActivity {
ActivityTwoVarBinding binding;
int A,B,C,D,E,F;
String N,M;
int X,Y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding=ActivityTwoVarBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().hide();
A=Integer.parseInt(binding.a.getText().toString());
B=Integer.parseInt(binding.b.getText().toString());
C=Integer.parseInt(binding.c.getText().toString());
D=Integer.parseInt(binding.d.getText().toString());
E=Integer.parseInt(binding.e.getText().toString());
F=Integer.parseInt(binding.f.getText().toString());
binding.calbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
X= ((C*E)-(B*F))/((A*E)-(B*D));
Y= ((C*D)-(A*F))/((B*D)-(A*E));
N=Integer.toString(X);
M=Integer.toString(Y);
binding.valX.setText(N);
binding.valY.setText(M);
}
});
}
}
error is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shubh.college_calculator/com.shubh.college_calculator.Two_var_act}: java.lang.NumberFormatException: For input string: ""
Solution
The problem is Integer.parseInt(binding.a.getText().toString())
and all the subsequent statements eg: binding.b.getText().toString()
is empty when the activity onCreate
method is called. Integer.parseInt
needs a valid number in a string to be able to parse it.
To fix this, check if the value is empty or not, maybe using binding.a.getText().toString().length() != 0
. If no, then go ahead and do the parsing.
Answered By - Arjis Chakraborty
Answer Checked By - Katrina (JavaFixing Volunteer)