Java:java.lang.NumberFormatException

时间:2013-10-03 02:24:28

标签: java exception compiler-errors

import java.io.*;

public class tempdetection {
public static int celciustofarenheit(int temp){
    int fTemp = ((9 * temp)/5) + 32;
    return fTemp;
}


public static void examineTemperature(int temp){
    System.out.println("\nTemperature is " + temp + " in celcius. Hmmm...");

    int fTemp = celciustofarenheit(temp);
    System.out.println("\nThats " + fTemp + " in Farenheit...");

    if(fTemp<20)
        System.out.println("\n***Burrrr. Its cold...***\n\n");
    else if(fTemp>20 || fTemp<50)
        System.out.println("\n***The weather is niether too hot nor too cold***\n\n");
    else if(fTemp>50)
        System.out.println("\n***Holy cow.. Its scorching.. Too hot***\n\n");
}


public static void main(String[] args) throws IOException {
    int temperature;
    char c;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    do{
        System.out.println("Input:\n(Consider the input is from the sensor)\n");

        temperature = Integer.parseInt(br.readLine());

        examineTemperature(temperature);
        System.out.println("Does the sensor wanna continue giving input?:(y/n)\n");
        c = (char) br.read();
    }while(c!= 'N' && c!='n');          //if c==N that is no then stop



}

}

这是完整的代码人..我仍然得到我的答案..我已经在网上搜索了很多但无济于事。还要感谢谁已经帮助了但是那个解决了我的问题问题..温度是int ..为什么我应该转换为字符串。?? 此外,我尝试尝试捕获其中一个成员,但然后检查温度(温度)抛出错误说它没有初始化..

Input:
(Consider the input is from the sensor)

45

Temperature is 45 in celcius. Hmmm...

Thats 113 in Farenheit...

***The weather is niether too hot nor too cold***


Does the sensor wanna continue giving input?:(y/n)

N
Input:
(Consider the input is from the sensor)

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at tempdetection.main(tempdetection.java:33)

它也适用于fyn,直到它到达while循环..

4 个答案:

答案 0 :(得分:4)

在你的do / while循环条件中,||应为&&

do{
    System.out.println("Input:\n(Consider the input is from the sensor)\n");
    temperature = Integer.parseInt(br.readLine());
    examineTemperature(temperature);
    System.out.println("Does the sensor wanna continue giving input?:(y/n)\n");
    c = (char) br.read();
} while (c != 'N' && c != 'n');

答案 1 :(得分:1)

行中的错误

temperature = Integer.parseInt(br.readLine());

这是在输入中读取并尝试将其解析为整数。正如异常所示,输入不是数字,NumberFormatExceptionInteger.parseInt()期望参数为数字。

有多种方法可以解决此问题:

一种方式(我个人认为不是最好的)是捕捉异常并且什么都不做并继续

try
{
    temperature = Integer.parseInt(br.readLine());
    // and do any code that uses temperature
    //if you don't then temperature will not be assigned
}
catch (NumberFormatException nfex)
{}

更好的方法是在尝试解析之前检查输入字符串是否为数字

String input = br.readLine();
if(input.matches("\\d+"))  // Only ints so don't actually need to consider decimals
    //is a number... do relevant code
    temperature = Integer.parseInt(input);
else
    //not a number
    System.out.println("Not a number that was input");

答案 2 :(得分:0)

您无法解析无法转换为整数的字符串。

Integer.parseInt(String s)抛出此异常;

答案 3 :(得分:0)

如果您已看到数字格式异常,如下所示:

     java.lang.NumberFormatException: For input string: ""
     java.lang.Integer.parseInt(Integer.java:627)
     com.tejveer.hiandroiddevelopers.MainActivity.onCreate(MainActivity.java:24)
     android.app.Activity.performCreate(Activity.java:7802)    

会创建此类错误,然后应用程序将崩溃。

然后通过编写单独的方法解决了这个问题

像这样

public void thisIsMethod(View textView){
    EditText edtFn = findViewById(R.id.efn);
    EditText edtSN = findViewById(R.id.esn);
     int mResult = Integer.parseInt(edtFn.getText().toString())*
     Integer.parseInt(edtSN.getText().toString());
}

此方法写在此块之外

protected void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);  }