英文:
Casting String to Integer not working, throwing NumberFormatException
问题
// 以下是要翻译的内容:
The value of walletBalance is a string, eg "150.0".
I am trying to display an error message in form of a toast in another activity(SuccessActivity) is the amount to be withdrawn from the user is less than the wallet balance. I keep getting NumberFormatException error for the value of i, so i decided to use a try catch block but it still doesnt work. Here is the method below.
private void checkWalletBalance(int amount, Context context){
String walletBalance = Preferences.getBalance(context);
try {
int i = Integer.parseInt(walletBalance.trim());
if(amount < i){
Intent intent = new Intent(getBaseContext(), SuccessActivity.class);
startActivity(intent);
Toast.makeText(ActivityHome.this,"Insufficient Wallet Balance",Toast.LENGTH_LONG).show();
}
}
catch (NumberFormatException nfe){
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
Alos, I want to display a toast in the success Activity, If the condition i true. here is the code in success activity to display the toast message.
private void insufficientError(){
Intent intent = getIntent();
intent.getExtras();
Toast.makeText(SuccessActivity.this,"Insufficient Balance",Toast.LENGTH_LONG).show();
}
英文:
The value of walletBalance is a string, eg "150.0".
I am trying to display an error message in form of a toast in another activity(SuccessActivity) is the amount to be withdrawn from the user is less than the wallet balance. I keep getting NumberFormatException error for the value of i, so i decided to use a try catch block but it still doesnt work. Here is the method below.
private void checkWalletBalance(int amount, Context context){
String walletBalance = Preferences.getBalance(context);
try {
int i = Integer.parseInt(walletBalance.trim());
if(amount < i){
Intent intent = new Intent(getBaseContext(), SuccessActivity.class);
startActivity(intent);
Toast. makeText(ActivityHome.this,"Insufficient Wallet Balance",Toast. LENGTH_LONG).show();
}
}
catch (NumberFormatException nfe){
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
Alos, I want to display a toast in the success Activity, If the condition i true. here is the code in success activity to display the toast message.
private void insufficientError(){
Intent intent = getIntent();
intent.getExtras();
Toast.makeText(SuccessActivity.this,"Insufficient Balance",Toast.LENGTH_LONG).show();
}
答案1
得分: 1
像 @Blackbelt 评论的那样,您正在尝试解析一个双精度字符串,而不是一个整数。
因此,您需要执行以下操作:
double amount = Double.parseDouble(walletBalance.trim());
英文:
Like @Blackbelt commented, you are trying to parse a double string and not an integer.
Therefore, you need to do the following:
double amount = Double.parseDouble(walletBalance.trim());
答案2
得分: 1
你可以使用以下任一方法:
Double.valueOf(walletBalance.trim());
Double.parseDouble(walletBalance.trim());
然后,如果你想将它们转换为整数(Integer/int),可以像这样进行:
Integer i = Double.valueOf(walletBalance.trim()).intValue();
int i = (int) Double.parseDouble(walletBalance.trim());
英文:
You could use either of the following-
Double.valueOf(walletBalance.trim());
Double.parseDouble(walletBalance.trim());
And then if you want to convert them to Integer/int like this -
Integer i = Double.valueOf(walletBalance.trim()).intValue();
int i = (int) Double.parseDouble(walletBalance.trim());
答案3
得分: 0
NumberFormatException - 表示应用程序尝试将字符串转换为数字类型之一,但字符串的格式不正确。
不要这样做
int i = Integer.parseInt(walletBalance.trim()); //walletBalance.trim() ==150.0
请这样做
int i = (int) Double.parseDouble(walletBalance.trim());
英文:
NumberFormatException - Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
Don't
int i = Integer.parseInt(walletBalance.trim()); //walletBalance.trim() ==150.0
Do
int i = (int) Double.parseDouble(walletBalance.trim());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论