0

I'm also getting "tempinput cannot be resolved to a variable", how can I fix that? I am new to Java btw.

import java.util.Scanner;

public class TemperatureCheck {
    public static void main(String [] args){

        Scanner cin = new Scanner(System.in);
        try{
            while(cin.nextLine().isEmpty()){
                System.out.print("Input a tempature to check: ");
                int tempinput = cin.nextInt();
            }

            if(tempinput >= 65){
                System.out.println("It is hot outside");
            }else if(tempinput >= 50 && tempinput <= 64){
                System.out.println("It is warm outside");
            }else{
                    System.out.println("It is cold outside");
            }                
        }finally{
            cin.close();
        }
        
    }
}
  • Read https://stackoverflow.com/questions/38177140/what-is-scope-in-java – tgdavies Jun 17 '22 at 03:29
  • Also, the `next` methods on `Scanner` *consume* the input, so think about what that means for the test in your while loop. – tgdavies Jun 17 '22 at 03:31
  • The variable "tempinput" is defined inside the `while` loop, so `int tempinput` is inside `while () { ... }`. You have other code which is outside of that "while" loop, code which is also trying to use `tempinput`. The compiler is telling you that it doesn't know what `tempinput` is (it's referring to the uses outside of the while loop). Depending where you define a variable, some parts of your code can "see" that variable, and other parts cannot. – Kaan Jun 17 '22 at 04:12
  • Just [check if `tempinput` is not a number](https://stackabuse.com/java-check-if-string-is-a-number/) using a while loop over the whole code in main. Java follows [lower camel case](https://www.google.com/amp/s/www.theserverside.com/feature/Java-naming-conventions-explained%3famp=1) for it's naming conventions. – Alias Cartellano Jun 17 '22 at 21:08

0 Answers0