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

huangapple go评论79阅读模式
英文:

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 &lt; i){
                        Intent intent = new Intent(getBaseContext(), SuccessActivity.class);
                        startActivity(intent);
                      
                        Toast. makeText(ActivityHome.this,&quot;Insufficient Wallet Balance&quot;,Toast. LENGTH_LONG).show();
                    }
        }
        catch (NumberFormatException nfe){
            System.out.println(&quot;NumberFormatException: &quot; + 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,&quot;Insufficient  Balance&quot;,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());

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

发表评论

匿名网友

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

确定