2013-10-03 2 views
0
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 .. 그래서 내가 문자열로 변환해야합니다. ?? 부재 중 하나에 의해 지정된 그런데 examineTemperature (온도)는 초기화되지 말하는 N 오류 발생으로 또한 난 .. 시도 캐치 시도 또한 Java : java.lang.NumberFormatException

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이 반복하면서이 ... 때까지

+0

'c = (char) br.readLine();'시도해주세요. –

+0

@RongNK : 아니요, 오류가 실제로 여기에 있습니다 :'c '='N '|| c! = 'n' '. 그'||'는'&&'이어야합니다. – Mac

+0

나는 변화했지만 아무 소용이 .. @Mac –

답변

1

라인에 오류

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

이것은 입력을 읽고 그것을 정수로 분석하려고합니다. 예외가 암시 하듯이 입력은 숫자가 아니기 때문에 NumberFormatException입니다. Integer.parseInt()은 인수가 숫자가 될 것으로 예상합니다.

이 해결하기 위해 여러 가지 방법이 있습니다 :

방법 중 하나를 예외를 catch하는 것입니다 그냥 아무것도하지 않고 계속

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"); 
+0

온도가 초기화되지 않았다는 말은 오류가 발생한다. 온도는 사용자로부터 입력을 받는다. 그리고 두 번째 방법은 i이다. 왜 내가 문자열을 변환 해야하는 이유는 내가 전달하고자하는 int입니다 .. 내가 전체 코드를 보냈습니다 .. 유 검사 할 수 있습니다 .. @ Java 악마 –

+0

예, 그때 입력을 구문 분석에 실패하면 온도가 초기화되지 않았습니다. 내 편집을 참조하십시오. 그리고 당신은 정규 표현식과 일치시킬 수 있도록 문자열을 읽는 것을 말하고있었습니다. 정말로 당신이 가지고있는 주된 문제는 당신이 'N'을 입력 할 때 편지에서 숫자를 만들려고 노력하고 있다는 것입니다. –

+0

안녕하세요! 고마워요....................... –

4

당신의 할 일에/루프의 상태 동안 ||&&해야한다 :

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'); 
+0

나는 실제 문제가 @Mac thats라고 생각하지 않는다. –

0

정수로 변환 할 수없는 문자열을 구문 분석 할 수 없습니다.

Integer.parseInt (String s)이 예외를 throw합니다.

+0

몇 가지 내용을 입력해야합니다. 답이 명확하지 않습니다. –

+0

내가 개선 할거야, @ RongNK – ThiepLV

관련 문제