应用程序正在崩溃,我知道这与;java.lang.NumberFormatException:无效的int:"$19.00“&引用;(在日志中),但我不知道如何修复它
它基本上是一个计算器应用程序。当用户返回编辑他们以前编辑过的EditText并第二次单击“计算”按钮时,就会出现问题。我知道答案就在我的脸上,但我真的对此知之甚少,所以我找不到解决办法。我被困了两个星期。提前谢谢你的帮助,我知道这是一个可怕的问题,但我真的被卡住了。替换代码是非常感谢,因为我是一个新手 .java代码-应用程序正在崩溃,我知道这与;java.lang.NumberFormatException:无效的int:"$19.00“&引用;(在日志中),但我不知道如何修复它,java,android,android-layout,android-studio,r.java-file,Java,Android,Android Layout,Android Studio,R.java File,它基本上是一个计算器应用程序。当用户返回编辑他们以前编辑过的EditText并第二次单击“计算”按钮时,就会出现问题。我知道答案就在我的脸上,但我真的对此知之甚少,所以我找不到解决办法。我被困了两个星期。提前谢谢你的帮助,我知道这是一个可怕的问题,但我真的被卡住了。替换代码是非常感谢,因为我是一个新手 .java代码- package washingtondeli.groupboxlunchesestimateandordering; import android.support.v7.app
package washingtondeli.groupboxlunchesestimateandordering;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
public class CapitolhillActivity extends AppCompatActivity {
Button mButton;
EditText sand1;
EditText sand2;
EditText sand3;
EditText sand4;
EditText sand5;
EditText sand6;
EditText sand7;
EditText extra1;
EditText extra2;
EditText extra3;
EditText extra4;
EditText extra5;
EditText extra6;
TextView result1;
TextView result2;
TextView result3;
TextView result4;
TextView result5;
TextView result6;
TextView result7;
TextView extraresult1;
TextView extraresult2;
TextView extraresult3;
TextView extraresult4;
TextView extraresult5;
TextView extraresult6;
CheckBox delivery;
TextView subtotal;
TextView total;
int d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capitolhill);
sand1= (EditText)findViewById(R.id.sand1);
sand2= (EditText)findViewById(R.id.sand2);
sand3= (EditText)findViewById(R.id.sand3);
sand4= (EditText)findViewById(R.id.sand4);
sand5= (EditText)findViewById(R.id.sand5);
sand6= (EditText)findViewById(R.id.sand6);
sand7= (EditText)findViewById(R.id.sand7);
extra1= (EditText)findViewById(R.id.extra1);
extra2= (EditText)findViewById(R.id.extra2);
extra3= (EditText)findViewById(R.id.extra3);
extra4= (EditText)findViewById(R.id.extra4);
extra5= (EditText)findViewById(R.id.extra5);
extra6= (EditText)findViewById(R.id.extra6);
result1= (TextView)findViewById(R.id.result1);
result2= (TextView)findViewById(R.id.result2);
result3= (TextView)findViewById(R.id.result3);
result4= (TextView)findViewById(R.id.result4);
result5= (TextView)findViewById(R.id.result5);
result6= (TextView)findViewById(R.id.result6);
result7= (TextView)findViewById(R.id.result7);
extraresult1= (TextView)findViewById(R.id.extraresult1);
extraresult2= (TextView)findViewById(R.id.extraresult2);
extraresult3= (TextView)findViewById(R.id.extraresult3);
extraresult4= (TextView)findViewById(R.id.extraresult4);
extraresult5= (TextView)findViewById(R.id.extraresult5);
extraresult6= (TextView)findViewById(R.id.extraresult6);
subtotal= (TextView) findViewById(R.id.subtotal);
total= (TextView) findViewById(R.id.total);
final CheckBox delivery= (CheckBox) findViewById(R.id.checkBox);
delivery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (delivery.isChecked()) {
d = 25;
} else {
d = 0;
}
}
});
mButton = (Button)findViewById(R.id.calc);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (delivery.isChecked()) {
d = 25;
} else {
d = 0;
}
calculate2();
}
});
}
public void calculate2() {
//get entered texts from the edittexts,and convert to integers.
Integer value1 = Integer.parseInt(sand1.getText().toString());
Integer value2 = Integer.parseInt(sand2.getText().toString());
Integer value3 = Integer.parseInt(sand3.getText().toString());
Integer value4 = Integer.parseInt(sand4.getText().toString());
Integer value5 = Integer.parseInt(sand5.getText().toString());
Integer value6 = Integer.parseInt(sand6.getText().toString());
Integer value7 = Integer.parseInt(sand7.getText().toString());
Integer value8 = Integer.parseInt(extra1.getText().toString());
Integer value9 = Integer.parseInt(extra2.getText().toString());
Integer value10 = Integer.parseInt(extra3.getText().toString());
Integer value11 = Integer.parseInt(extra4.getText().toString());
Integer value12 = Integer.parseInt(extra5.getText().toString());
Integer value13 = Integer.parseInt(extra6.getText().toString());
Integer valuesubtotal = Integer.parseInt(subtotal.getText().toString());
Integer valuetotal = Integer.parseInt(total.getText().toString());
//do the calculation
Double calculatedValue1 = (9.5*value1);
Double calculatedValue2 = (9.5*value2);
Double calculatedValue3 = (9.5*value3);
Double calculatedValue4 = (9.5*value4);
Double calculatedValue5 = (9.5*value5);
Double calculatedValue6 = (9.5*value6);
Double calculatedValue7 = (9.5*value7);
Double calculatedValue8 = (2.25*value8);
Double calculatedValue9=(1.5*value9) ;
Double calculatedValue10=(3.5*value10) ;
Double calculatedValue11 =(3.95*value11) ;
Double calculatedValue12 = (3.5*value12);
Double calculatedValue13 = (2.95*value13);
Double calculatedsubtotal = (d+calculatedValue1+calculatedValue2+calculatedValue3+calculatedValue4+calculatedValue5+calculatedValue6+calculatedValue7+calculatedValue8+calculatedValue9+calculatedValue10+calculatedValue11+calculatedValue12+calculatedValue13);
Double calculatedtotal = ((.1*calculatedsubtotal)+calculatedsubtotal);
//set the value to the textview, to display on screen.
result1.setText("$"+String.format( "%.2f", calculatedValue1 ));
result2.setText("$"+ String.format( "%.2f", calculatedValue2 ));
result3.setText("$"+ String.format( "%.2f", calculatedValue3 ));
result4.setText("$"+ String.format( "%.2f", calculatedValue4 ));
result5.setText("$"+ String.format( "%.2f", calculatedValue5 ));
result6.setText("$"+ String.format( "%.2f", calculatedValue6 ));
result7.setText("$"+ String.format( "%.2f", calculatedValue7 ));
extraresult1.setText("$"+ String.format( "%.2f", calculatedValue8 ));
extraresult2.setText("$"+ String.format( "%.2f", calculatedValue9 ));
extraresult3.setText("$"+ String.format( "%.2f", calculatedValue10 ));
extraresult4.setText("$"+ String.format( "%.2f", calculatedValue11));
extraresult5.setText("$"+ String.format( "%.2f", calculatedValue12));
extraresult6.setText("$"+ String.format( "%.2f", calculatedValue13));
subtotal.setText("$"+ String.format( "%.2f", calculatedsubtotal ));
total.setText("$"+ String.format( "%.2f", calculatedtotal ));
}
}
删除你的$sign。这是问题的根源,因为它不是一个数字。同时将变量类型更改为double。删除$符号。这是问题的根源,因为它不是一个数字。同时将变量类型更改为double。它第二次崩溃,因为$被添加到编辑文本值中,当您在该编辑文本上执行getString时,它将为您提供附加有$符号的字符串。第二次计算时,必须删除$symbol 根据您的代码显示一个值
String value=subtotal.getText().toString();
String replace=value.replace("$","");
Integer valuesubtotal = Integer.parseInt(replace);
它第二次崩溃,因为$被添加到编辑文本值中,当您对该编辑文本执行getString操作时,它将为您提供附加有$符号的字符串。第二次计算时,必须删除$symbol 根据您的代码显示一个值
String value=subtotal.getText().toString();
String replace=value.replace("$","");
Integer valuesubtotal = Integer.parseInt(replace);
在调用(textview/editText).getText().toString()之前,删除$symbol并使用double代替int
试试这个:
String total = "$10.5";
String strAmount = total.replace("$","");
double value = Double.valueOf(strAmount);
Log.e("VALUE", String.valueOf(value));
例如:
public void calculate2() {
//get entered texts from the edittexts,and convert to integers.
Double value1 = moneyToDouble(sand1.getText().toString());
Double value2 = moneyToDouble(sand2.getText().toString());
Double value3 = moneyToDouble(sand3.getText().toString());
Double value4 = moneyToDouble(sand4.getText().toString());
Double value5 = moneyToDouble(sand5.getText().toString());
Double value6 = moneyToDouble(sand6.getText().toString());
Double value7 = moneyToDouble(sand7.getText().toString());
Double value8 = moneyToDouble(extra1.getText().toString());
Double value9 = moneyToDouble(extra2.getText().toString());
Double value10 = moneyToDouble(extra3.getText().toString());
Double value11 = moneyToDouble(extra4.getText().toString());
Double value12 = moneyToDouble(extra5.getText().toString());
Double value13 = moneyToDouble(extra6.getText().toString());
Double valuesubtotal = moneyToDouble(subtotal.getText().toString());
Double valuetotal = moneyToDouble(total.getText().toString());
//do the calculation
Double calculatedValue1 = (9.5*value1);
Double calculatedValue2 = (9.5*value2);
Double calculatedValue3 = (9.5*value3);
Double calculatedValue4 = (9.5*value4);
Double calculatedValue5 = (9.5*value5);
Double calculatedValue6 = (9.5*value6);
Double calculatedValue7 = (9.5*value7);
Double calculatedValue8 = (2.25*value8);
Double calculatedValue9=(1.5*value9) ;
Double calculatedValue10=(3.5*value10) ;
Double calculatedValue11 =(3.95*value11) ;
Double calculatedValue12 = (3.5*value12);
Double calculatedValue13 = (2.95*value13);
Double calculatedsubtotal = (d+calculatedValue1+calculatedValue2+calculatedValue3+calculatedValue4+calculatedValue5+calculatedValue6+calculatedValue7+calculatedValue8+calculatedValue9+calculatedValue10+calculatedValue11+calculatedValue12+calculatedValue13);
Double calculatedtotal = ((.1*calculatedsubtotal)+calculatedsubtotal);
//set the value to the textview, to display on screen.
result1.setText("$"+String.format( "%.2f", calculatedValue1 ));
result2.setText("$"+ String.format( "%.2f", calculatedValue2 ));
result3.setText("$"+ String.format( "%.2f", calculatedValue3 ));
result4.setText("$"+ String.format( "%.2f", calculatedValue4 ));
result5.setText("$"+ String.format( "%.2f", calculatedValue5 ));
result6.setText("$"+ String.format( "%.2f", calculatedValue6 ));
result7.setText("$"+ String.format( "%.2f", calculatedValue7 ));
extraresult1.setText("$"+ String.format( "%.2f", calculatedValue8 ));
extraresult2.setText("$"+ String.format( "%.2f", calculatedValue9 ));
extraresult3.setText("$"+ String.format( "%.2f", calculatedValue10 ));
extraresult4.setText("$"+ String.format( "%.2f", calculatedValue11));
extraresult5.setText("$"+ String.format( "%.2f", calculatedValue12));
extraresult6.setText("$"+ String.format( "%.2f", calculatedValue13));
subtotal.setText("$"+ String.format( "%.2f", calculatedsubtotal ));
total.setText("$"+ String.format( "%.2f", calculatedtotal ));
}
private Double moneyToDouble(String total){
String strAmount = total.replace("$","");
double value = Double.valueOf(strAmount);
return value;
}
在调用(textview/editText).getText().toString()之前,删除$symbol并使用double代替int
试试这个:
String total = "$10.5";
String strAmount = total.replace("$","");
double value = Double.valueOf(strAmount);
Log.e("VALUE", String.valueOf(value));
例如:
public void calculate2() {
//get entered texts from the edittexts,and convert to integers.
Double value1 = moneyToDouble(sand1.getText().toString());
Double value2 = moneyToDouble(sand2.getText().toString());
Double value3 = moneyToDouble(sand3.getText().toString());
Double value4 = moneyToDouble(sand4.getText().toString());
Double value5 = moneyToDouble(sand5.getText().toString());
Double value6 = moneyToDouble(sand6.getText().toString());
Double value7 = moneyToDouble(sand7.getText().toString());
Double value8 = moneyToDouble(extra1.getText().toString());
Double value9 = moneyToDouble(extra2.getText().toString());
Double value10 = moneyToDouble(extra3.getText().toString());
Double value11 = moneyToDouble(extra4.getText().toString());
Double value12 = moneyToDouble(extra5.getText().toString());
Double value13 = moneyToDouble(extra6.getText().toString());
Double valuesubtotal = moneyToDouble(subtotal.getText().toString());
Double valuetotal = moneyToDouble(total.getText().toString());
//do the calculation
Double calculatedValue1 = (9.5*value1);
Double calculatedValue2 = (9.5*value2);
Double calculatedValue3 = (9.5*value3);
Double calculatedValue4 = (9.5*value4);
Double calculatedValue5 = (9.5*value5);
Double calculatedValue6 = (9.5*value6);
Double calculatedValue7 = (9.5*value7);
Double calculatedValue8 = (2.25*value8);
Double calculatedValue9=(1.5*value9) ;
Double calculatedValue10=(3.5*value10) ;
Double calculatedValue11 =(3.95*value11) ;
Double calculatedValue12 = (3.5*value12);
Double calculatedValue13 = (2.95*value13);
Double calculatedsubtotal = (d+calculatedValue1+calculatedValue2+calculatedValue3+calculatedValue4+calculatedValue5+calculatedValue6+calculatedValue7+calculatedValue8+calculatedValue9+calculatedValue10+calculatedValue11+calculatedValue12+calculatedValue13);
Double calculatedtotal = ((.1*calculatedsubtotal)+calculatedsubtotal);
//set the value to the textview, to display on screen.
result1.setText("$"+String.format( "%.2f", calculatedValue1 ));
result2.setText("$"+ String.format( "%.2f", calculatedValue2 ));
result3.setText("$"+ String.format( "%.2f", calculatedValue3 ));
result4.setText("$"+ String.format( "%.2f", calculatedValue4 ));
result5.setText("$"+ String.format( "%.2f", calculatedValue5 ));
result6.setText("$"+ String.format( "%.2f", calculatedValue6 ));
result7.setText("$"+ String.format( "%.2f", calculatedValue7 ));
extraresult1.setText("$"+ String.format( "%.2f", calculatedValue8 ));
extraresult2.setText("$"+ String.format( "%.2f", calculatedValue9 ));
extraresult3.setText("$"+ String.format( "%.2f", calculatedValue10 ));
extraresult4.setText("$"+ String.format( "%.2f", calculatedValue11));
extraresult5.setText("$"+ String.format( "%.2f", calculatedValue12));
extraresult6.setText("$"+ String.format( "%.2f", calculatedValue13));
subtotal.setText("$"+ String.format( "%.2f", calculatedsubtotal ));
total.setText("$"+ String.format( "%.2f", calculatedtotal ));
}
private Double moneyToDouble(String total){
String strAmount = total.replace("$","");
double value = Double.valueOf(strAmount);
return value;
}
请将您的问题与您的例外情况一起发布。看起来您没有正确分析用户的输入。可能您确实想使用NumberFormat currency实例。请将您的问题与异常stacktrace一起发布。看起来你没有正确解析用户的输入。也许你真的想使用NumberFormat currency实例。呃,。我在这方面真的很差,你介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事是不好的,只是已经过了两周,问了三个问题,我想不出来。我真的很不擅长这件事,你介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事是不好的,只是已经两周了,三个问题之后,我无法理解我真的很不擅长,你介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事是不好的,只是已经两周了,三个问题之后,我都想不出来了,我到底把这个放在哪里了。。。抱歉,在这一行之前,我真的丢失了
Integer valuesubtotal=Integer.parseInt(subtotal.getText().toString())代码>我对每一个值都这样做?每一个在itI中带有$symbol的值我在这方面非常糟糕,你介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事是不好的,只是已经两周了,三个问题之后,我都想不出来了,我到底把这个放在哪里了。。。抱歉,在这一行之前,我真的丢失了Integer valuesubtotal=Integer.parseInt(subtotal.getText().toString())代码>我对每一个值都这样做?每一个在itI中带有$symbol的值我在这方面非常糟糕,你介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事很不好,只是已经两周了,三个问题之后,我想不出来了,我要把它复制粘贴到我的计算中吗?我必须为每个值做底部,对吗?是的,复制并粘贴calculation2方法和moneyToDouble方法,告诉我你是否对此有其他问题。这很有效!非常感谢你!如果你不介意的话,我还有一个问题。单击“计算”时,屏幕将滚动到我在scrollview中编辑的最后一个编辑文本。我希望在单击“计算”后屏幕不会滚动。:),请发布您的活动布局(xml)以帮助您。我在这方面非常差,您介意编辑我的代码吗,因为我真的迷路了。我知道让别人帮我做这件事很不好,只是已经两周了,三个问题之后,我想不出来了,我要把它复制粘贴到我的计算中吗?我必须为每个值做底部,对吗?是的,复制并粘贴calculation2方法和moneyToDouble方法,告诉我你是否对此有其他问题。这很有效!非常感谢你!如果你不介意的话,我还有一个问题。单击“计算”时,屏幕将滚动到我在scrollview中编辑的最后一个编辑文本。我希望在单击“计算”后屏幕不会滚动。:),请发布您的活动布局(xml)以帮助您完成此操作。