0

I'm a beginner programmer, I have been writing code for about three weeks now. I want to make a simple program that asks for user input temperature and tells whether or not the user has fever (temperature higher than 39). I also want to validate the user input, meaning that if the user types "poop" or "!@·R%·%" (symbol gibberish), the program will output the phrase "invalid input". I'm trying to use the try/catch statements, this is my code:

package feverPack;

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{

        try{
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader stdIn = new BufferedReader(inStream);

        System.out.println("please input patient temperature in numbers");
        String numone =stdIn.readLine();}

        catch (IOException e) {
            System.out.println("invalid input");
        }
        catch (NumberFormatException e){
            System.out.println("invalid input");
        }
        int temp = Integer.parseInt(numone) ;


        System.out.println("Your temperature is " + temp + "ºC");


        if (temp > 39) {
            System.out.println("You have fever! Go see a doctor!");
        }
        else{
            System.out.println("Don't worry, your temperature is normal");
        }
    }
}

There is an error in line 22 (when i transform numone into a temp) saying "numone cannot be resolved to a variable", as I am a beginner I don't really know what to do, please help.

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • 1
    The answer has been given already, but look here for some more information: http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm – Dylan Meeus Nov 07 '14 at 02:36
  • so i did what the answer said, added that extra line, no more errors, still the program doesn't fulfill its function, by this i mean, when i type phrases or letters as input the program still crashes instead of going with the catch statement – rodrigo villarreal Nov 07 '14 at 02:48
  • place `int temp = Integer.parseInt(numone) ;` inside the `try` block – Niroshan Nov 07 '14 at 02:57

1 Answers1

1

Move the declaration of numone outside the try block. Basically, numone was declared within the scope of the try block and was not available outside the scope of the try block, so moving it out would give it a wider visibility.

String numone = null;
int temp = 0;
try
{
...
numone = stdIn.readLine();
temp = Integer.parseInt(numone) ;
System.out.println("Your temperature is " + temp + "ºC");
if (temp > 39) {
      System.out.println("You have fever! Go see a doctor!");
 }
else{
    System.out.println("Don't worry, your temperature is normal");
}
}
catch(..)
{
...
}
BatScream
  • 19,260
  • 4
  • 52
  • 68
  • tried that man, it comes up with a second error saying "syntax error on tokens: delete this tokens", take into account I have checked and String is written correctly. – rodrigo villarreal Nov 07 '14 at 02:37
  • Can you please Update your question with what you have tried. – BatScream Nov 07 '14 at 02:39
  • scratch that, dumb mistake, but still when i write "no" as an input the program crashes – rodrigo villarreal Nov 07 '14 at 02:40
  • Yes thats a programming error, you expect `numone` to be an `integer` and you enter a `string` which cannot be parsed into an integer. It would throw you an `NumberFormatException`. – BatScream Nov 07 '14 at 02:42
  • i thought that was the whole point of catch, to avoid the crashes and instead posting a message like i did in the code – rodrigo villarreal Nov 07 '14 at 02:44
  • works, one more question man, the program outputs the line "invalid input" as planned, but still outputs the rest of the phrases in the program (your temperature is ...) (don't worry...) as if i had written 0 as the input, do you know how i can change this? – rodrigo villarreal Nov 07 '14 at 02:54
  • perfect, is there a way to loop the code back to input in case of error? – rodrigo villarreal Nov 07 '14 at 02:59
  • I guess this question has been answered here, the answer to your next question lies here: http://stackoverflow.com/questions/19130217/how-to-loop-user-input-until-an-integer-is-inputted. If you still have specific doubts kindly open a new question. – BatScream Nov 07 '14 at 03:02