我有一个计算,我必须从用户用EditText给出的总数中减去5个变量,我的问题是:有没有办法排除任何值(et1,et2,et3,et4) ,et5)如果其中一个是0? 例如,如果用户将500作为“totaal”并且对于et1& et2为零,其余四个我想要从“totaal”中减去3 * 4
这是代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class Bereken extends AppCompatActivity {
private Double totaal = null;
TextView Txttotal;
Button k1;
Button k2;
Button k3;
TextView kies;
EditText ET1;
EditText ET2;
EditText ET3;
EditText ET4;
EditText ET5;
double temporary;
double randomdouble;
double formerval;
double totje;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bereken);
totaal = getIntent().getExtras().getDouble("Hannibal");
Txttotal = (TextView) findViewById(R.id.Tijdelijk);
Txttotal.setText(new DecimalFormat("##.##").format(totaal));
k1 = (Button)findViewById(R.id.optie1);
k2 = (Button)findViewById(R.id.optie2);
k3 = (Button)findViewById(R.id.optie3);
ET1 = (EditText)findViewById(R.id.editText7);
ET2 = (EditText)findViewById(R.id.editText8);
ET3 = (EditText)findViewById(R.id.editText9);
ET4 = (EditText)findViewById(R.id.editText10);
ET5 = (EditText)findViewById(R.id.editText11);
ET1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!ET1.getText().toString().equals("")) {
double et1 = Double.parseDouble(ET1.getText().toString());
double et2 = Double.parseDouble(ET2.getText().toString());
double et3 = Double.parseDouble(ET3.getText().toString());
double et4 = Double.parseDouble(ET4.getText().toString());
double et5 = Double.parseDouble(ET5.getText().toString());
totje = totaal - et1 - et2 - et3 - et4 - et5;
Txttotal.setText(new DecimalFormat("##.##").format(totje));
}else{
totaal = totaal + formerval;
Txttotal.setText(new DecimalFormat("##.##").format(totaal));
}
randomdouble = temporary;
}
});
ET2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
这是错误
02-16 17:05:11.148 18568-18568/dipp.verlinden E/AndroidRuntime: FATAL EXCEPTION: main
Process: dipp.verlinden, PID: 18568
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at dipp.verlinden.Bereken$1.afterTextChanged(Bereken.java:64)
at android.widget.TextView.sendAfterTextChanged(TextView.java:9195)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:11939)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1047)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:564)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:494)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:34)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:691)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:199)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:184)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:286)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
问题不在0,而是在这部分
double et1 = Double.parseDouble(ET1.getText().toString());
double et2 = Double.parseDouble(ET2.getText().toString());
double et3 = Double.parseDouble(ET3.getText().toString());
double et4 = Double.parseDouble(ET4.getText().toString());
double et5 = Double.parseDouble(ET5.getText().toString());
如果ET2,ET3,ET4,ET5中的任何一个为空字符串(“”)并且无法解析为double,则会发生错误。
因此,您需要通过以下条件进行检查:
if(!ET1.getText().toString().equals("") && !ET2.getText().toString().equals("") &&
!ET3.getText().toString().equals("") && !ET4.getText().toString().equals("") &&
!ET5.getText().toString().equals("")) {
//double parsing code here
} else {
//code to run if either of the fields are empty
}
你已经在为ET1做了这件事,但只有ET1,你必须为所有人做这件事,因为这些领域中的任何一个都可能是空的。我希望有所帮助。
答案 1 :(得分:0)
这绝不重要,任何减零的东西本身,totaal-et1
et1=0
是totaal
。只是让它进行数学计算,不要让你的代码过于复杂,试图做一些不会改变结果的东西。
您不会保存处理,因为使用0的基本数学比检查它们是否为0更快并重写数学。您不会节省空间,因为您的代码将会大小增加。
既然你已经包含了其余的代码,那么你的问题会更有意义。
string et1s =regex.replace(ET1.getText().toString(),"[^0-9.,]","");
double et1 = et1s ==string.Empty :0?Double.parseDouble(et1s);
第一行接受输入,删除所有不是0-9或十进制数字的数字。或',',第二行获取第一行的结果并检查是否有某些东西然后尝试将其转换为小数。一句警告,如果用户输入多个小数或只是小数而没有实际数字,这仍然会导致问题。它仍然可以改进,但它将解决你遇到的许多问题。