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



}

}

This is the complete code guys.. I still din get my answer.. I've searched a lot on net but to no avail.. Also thanks for who've already helped but that din resolve my problem.. Temperature is int.. SO why i should convert to string.?? Also i tried try catch as specified by one of the member but then examineTemperature(temperature) throws n error saying its not initialized..

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)

Also it works fyn until it reaches while loop..

3
  • Try c = (char) br.readLine(); Commented Oct 3, 2013 at 2:31
  • @RongNK: nope, the error's actually here: c!= 'N' || c!='n'. That || should be an &&. Commented Oct 3, 2013 at 2:32
  • I changed but to no avail.. @Mac Commented Oct 14, 2013 at 10:57

4 Answers 4

4

In your do/while loop's condition, the || should be an &&:

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');
Sign up to request clarification or add additional context in comments.

1 Comment

I dont think so thats the actual problem @Mac
1

The error in the line

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

This is reading in the input and trying to parse it as an Integer. As the exception suggests the input is not a number i.e. NumberFormatException as Integer.parseInt() expects the argument to be a number.

There are multiple ways to fix this:

One way (I personally believe not the best) is to catch the exception and just do nothing and continue

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

A better way would be to check the input string is a number before trying to parse

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

3 Comments

Try and catch gives an error saying temperature not initialized.. Though temperature is getting a input from user.. And the second method i din understand since why should i convert it to string since the value that i wanna pass is a int.. I've sent the whole code.. U can examine.. @Java Devil
Yes, if it fails to parse the input then temperature will not have been initialised. See my edit. And I was saying to read in the String so that you could match it with a regex. Really the main problem you are having is you are trying to create a number from a letter when you input 'N'
Hey it worked!! Thanks alot.. I used try catch to resolve it thank u!!
0

You can't parse a string that can't convert to a integer number.

Integer.parseInt(String s) throw this exception;

1 Comment

I will improve, @RongNK
0

If you have seen Number format exception like this:

     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)    

this type of error is created then application will be crashed.

then resolved this problem by writing the separate method

like this

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

This method write outside this block

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.