0

So I am currently learning java and its my first time to encounter this problem. I don't know how to fix this error. It says that passwordInput cannot be resolved to a variable and I dont know what does that mean. This is my whole code:

import java.util.Scanner;
public class Password {

    public static void main(String[] args) {
        String username = "JohnPaul";
        int password = 11062223;
        
        System.out.println("PLEASE INSERT YOUR CARD TO LOGIN");
        System.out.println("Username : ");
        Scanner scan = new Scanner (System.in);
        String usernameInput = scan.nextLine();
        
        if(usernameInput.equals(username)) {
            System.out.println("Password : ");
            int passwordInput = scan.nextInt();
            scan.nextLine();
            System.out.println(passwordInput);
        }
        else
            System.out.println("User not found.");
        System.out.println(passwordInput);
        
    }

}
newbie
  • 1
  • 1
    The variable is declared in the `if` block, so it is unknown/out of scope in the corresponding `else` block . Consider declaring it before the `if` block. – Arnaud Oct 19 '20 at 13:12
  • 1
    What @Arnaud said is correct, with the exception that the corresponding call is not in the else block. – maloomeister Oct 19 '20 at 13:16
  • Also maybe take a look at https://stackoverflow.com/questions/5146197/variable-access-outside-of-if-statement – MDK Oct 19 '20 at 13:21

0 Answers0