将字符串转换为整数不起作用,抛出NumberFormatException。

huangapple 未分类评论51阅读模式
英文:

Casting String to Integer not working, throwing NumberFormatException

问题

  1. // 以下是要翻译的内容:
  2. The value of walletBalance is a string, eg "150.0".
  3. 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.
  4. private void checkWalletBalance(int amount, Context context){
  5. String walletBalance = Preferences.getBalance(context);
  6. try {
  7. int i = Integer.parseInt(walletBalance.trim());
  8. if(amount < i){
  9. Intent intent = new Intent(getBaseContext(), SuccessActivity.class);
  10. startActivity(intent);
  11. Toast.makeText(ActivityHome.this,"Insufficient Wallet Balance",Toast.LENGTH_LONG).show();
  12. }
  13. }
  14. catch (NumberFormatException nfe){
  15. System.out.println("NumberFormatException: " + nfe.getMessage());
  16. }
  17. }
  18. 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.
  19. private void insufficientError(){
  20. Intent intent = getIntent();
  21. intent.getExtras();
  22. Toast.makeText(SuccessActivity.this,"Insufficient Balance",Toast.LENGTH_LONG).show();
  23. }
英文:

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.

  1. private void checkWalletBalance(int amount, Context context){
  2. String walletBalance = Preferences.getBalance(context);
  3. try {
  4. int i = Integer.parseInt(walletBalance.trim());
  5. if(amount &lt; i){
  6. Intent intent = new Intent(getBaseContext(), SuccessActivity.class);
  7. startActivity(intent);
  8. Toast. makeText(ActivityHome.this,&quot;Insufficient Wallet Balance&quot;,Toast. LENGTH_LONG).show();
  9. }
  10. }
  11. catch (NumberFormatException nfe){
  12. System.out.println(&quot;NumberFormatException: &quot; + nfe.getMessage());
  13. }
  14. }

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.

  1. private void insufficientError(){
  2. Intent intent = getIntent();
  3. intent.getExtras();
  4. Toast.makeText(SuccessActivity.this,&quot;Insufficient Balance&quot;,Toast.LENGTH_LONG).show();
  5. }

答案1

得分: 1

像 @Blackbelt 评论的那样,您正在尝试解析一个双精度字符串,而不是一个整数。

因此,您需要执行以下操作:

  1. 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

你可以使用以下任一方法:

  1. Double.valueOf(walletBalance.trim());
  2. Double.parseDouble(walletBalance.trim());

然后,如果你想将它们转换为整数(Integer/int),可以像这样进行:

  1. Integer i = Double.valueOf(walletBalance.trim()).intValue();
  2. int i = (int) Double.parseDouble(walletBalance.trim());
英文:

You could use either of the following-

  1. Double.valueOf(walletBalance.trim());
  2. Double.parseDouble(walletBalance.trim());

And then if you want to convert them to Integer/int like this -

  1. Integer i = Double.valueOf(walletBalance.trim()).intValue();
  2. int i = (int) Double.parseDouble(walletBalance.trim());

答案3

得分: 0

NumberFormatException - 表示应用程序尝试将字符串转换为数字类型之一,但字符串的格式不正确。

不要这样做

  1. int i = Integer.parseInt(walletBalance.trim()); //walletBalance.trim() ==150.0

请这样做

  1. 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

  1. int i = Integer.parseInt(walletBalance.trim()); //walletBalance.trim() ==150.0

Do

  1. int i = (int) Double.parseDouble(walletBalance.trim());

huangapple
  • 本文由 发表于 2020年4月9日 14:16:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61115025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定