Java 浮点变量!=无效
如果使用(loanAmtValue!=null),它会抛出一个错误,即“Operator!=不能应用于float,null”。Plzz sum1想出了解决这个问题的办法。。如果该字段中没有给出值,应用程序将崩溃。所以我尝试使用空条件检查,这样即使没有给出值,应用程序也不会崩溃Java 浮点变量!=无效,java,android,Java,Android,如果使用(loanAmtValue!=null),它会抛出一个错误,即“Operator!=不能应用于float,null”。Plzz sum1想出了解决这个问题的办法。。如果该字段中没有给出值,应用程序将崩溃。所以我尝试使用空条件检查,这样即使没有给出值,应用程序也不会崩溃 public class SICalculatorActivity extends Activity implements View.OnClickListener { private Button submit
public class SICalculatorActivity extends Activity implements View.OnClickListener {
private Button submit;
private Button submit2;
SeekBar sb;
TextView yrs;
EditText loanAmt;
EditText roi;
TextView siResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
final int years;
sb = (SeekBar)findViewById(R.id.set_years);
yrs = (TextView)findViewById(R.id.years);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
yrs.setText(sb.getProgress()+" year(s)");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
addButtonClickListener();
}
private void addButtonClickListener() {
submit = (Button)findViewById(R.id.button);
submit.setOnClickListener(this);
submit2 = (Button)findViewById(R.id.button2);
submit2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
float loanAmtValue = 0;
float roiValue = 0;
double answer=0;
float y = sb.getProgress();
loanAmt = (EditText)findViewById(R.id.amt);
loanAmtValue = Float.parseFloat(loanAmt.getText().toString());
roi = (EditText)findViewById(R.id.roi);
roiValue = Float.parseFloat(roi.getText().toString());
if (loanAmtValue != null && roiValue != null){
case R.id.button:
answer = (loanAmtValue * roiValue * y) / 100;
siResult = (TextView) findViewById(R.id.result);
siResult.setText("Simple Interest for Amount Rs." + loanAmtValue + " and ROI " + roiValue + "% for "+y+" year(s) is = " + String.format("%.2f", answer));
loanAmt.setText("0");
roi.setText("0");
break;
}
}
else
{
siResult.setText("Please provide valid details");
}
}
}
float
是一种基本数据类型,而不是对象<代码>空检查用于对象
对于基本体,应使用默认值检查。对于float,默认值为0.0f
阅读如下:
空值用于对象。如果一个对象为null,那么这意味着这个变量不指向任何地方
Obj a = null // a points nowhere
a = new Obj() // now a points to a place in the memory (a references an Obj object in the memory
float不是一种对象类型,它是一种基本类型。但是,如果进行如下比较,请小心:x==0.0(浮点比较与比较整数不同!)。问题似乎出在这里:
loanAmtValue = Float.parseFloat(loanAmt.getText().toString());
如果loanAmt
不包含可转换为浮点的字符串,则此行将抛出NumberFormatException
在尝试转换为浮点之前,可以检查字符串是否为空:
if (loanAmt.getText() != null && !loanAmt.getText().isEmpty()) {
loanAmtValue = Float.parseFloat(loanAmt.getText().toString());
}
如果用户输入无效字符串,这仍然会引发异常,因此您必须捕获异常:
if (loanAmt.getText() != null && !loanAmt.getText().isEmpty()) {
try {
loanAmtValue = Float.parseFloat(loanAmt.getText().toString());
catch (NumberFormatException ex) {
loanAmtValue = 0.0; // or some other default value
}
}
要完成答案,您不能像这里每个人所说的那样检查浮点值是否为空。你所能做的就是,像“魔王”说的那样,检查它是否正确=0,如果要确保该值不是0(与null不一样!!!)。但是: 为了防止出现NumberFormatException,您还必须确保从编辑文本字段捕获的值是一个数字。您可以使用TextUtils执行此操作:
android.text.TextUtils.isDigitsOnly(CharSequence str).
在您的情况下,您应该这样做:
loanAmt = (EditText)findViewById(R.id.amt);
String loanAmtString = loanAmt.getText().toString();
if(android.text.TextUtils.isDigitsOnly(loanAmtString)){
loanAmtValue = Float.parseFloat(loanAmtString);
}
roi = (EditText)findViewById(R.id.roi);
String roiString = roi.getText().toString();
if(android.text.TextUtils.isDigitsOnly(roiString)){
roiValue = Float.parseFloat(roiString);
}
在我看来,有两种方法可以强制用户只输入浮点值
inputType
用于EditText
Pattern ptrn = Pattern.compile("^\\d*\\.\\d*$");
String enteredText = ...;
if(ptrn.matcher(enteredText).matches()){
// try to parse the string here
}
你认为什么是
“Operator!=不能应用于float,null”
的意思是什么?你不能比较float,因为它们是primative而不是object!请记住空值!=0(真的,不是开玩笑!),将if(loanAmtValue!=null&&roiValue!=null){
更改为if(loanAmtValue!=0.0f&&roiValue!=0.0f){
使用logcat,Luke!@user645,Der Golem是正确的。你应该说!=0
,因为浮点变量不能等于null
。因此,如果你的应用程序在你将其更改为!=0
时崩溃,那么你就会在if语句之后或内部发生错误。回答得好!一个小小的调整,我最喜欢的li之一ttle Android treats使用if(!TextUtils.isEmpty(loanAmt.getText())
而不是if(loanAmt.getText()!=null&&!loanAmt.getText().isEmpty())
:)@ScottW这是个好主意。我对Android实用程序类不太熟悉,所以我不知道这个方法的存在,尽管我并不感到惊讶。