簡體   English   中英

Java:java.lang.NumberFormatException

[英]Java: java.lang.NumberFormatException

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



}

}

這是完整的代碼家伙.. 我仍然能得到我的答案.. 我在網上搜索了很多但無濟於事.. 也感謝那些已經幫助但解決我的問題的人.. 溫度是整數。 . 所以為什么我要轉換成字符串。?? 我也嘗試過按成員之一指定的方法嘗試捕獲,但隨后examineTemperature(temperature) 拋出n 錯誤,說它沒有初始化..

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 循環..

在您的 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');

行中的錯誤

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

這是讀取輸入並嘗試將其解析為整數。 由於異常表明輸入不是數字,即NumberFormatException因為Integer.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");

您無法解析無法轉換為整數的字符串。

Integer.parseInt(String s) 拋出這個異常;

如果您看到過這樣的數字格式異常

     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);  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM