[英]how to solve Number Format Exception
I want to make app that solve Two variable two equation like;我想制作解决两个变量两个方程的应用程序;
aX+bY=c---(1) aX+bY=c---(1)
dX+eY=F---(2) dX+eY=F---(2)
And in My app i made six edit texts a,b,c,d,e,f.在我的应用程序中,我制作了六个编辑文本 a、b、c、d、e、f。 And other Text view to show X+,Y+,=.
和其他文本视图显示 X+、Y+、=。 and then what values i fetch i set into Text view of valX and valY
然后我获取的值设置到 valX 和 valY 的文本视图中
My code is我的代码是
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: ""
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(binding.a.getText().toString())
和所有后续语句,例如: binding.b.getText().toString()
在调用活动onCreate
方法时为空。 Integer.parseInt
needs a valid number in a string to be able to parse it. Integer.parseInt
需要字符串中的有效数字才能解析它。
To fix this, check if the value is empty or not, maybe using binding.a.getText().toString().length() != 0
.要解决此问题,请检查该值是否为空,可能使用
binding.a.getText().toString().length() != 0
。 If no, then go ahead and do the parsing.如果否,则 go 提前并进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.