0

I am working on a program using Java, and I am getting errors. The purpose of the program is to code a while loop with a nested if-else structure within an if-else structure.

  1. There is an error on line 33 that says "variable age might not have been initialized" even though it is declared in the main()?

  2. Am I correctly nesting the if-else structures inside the while?

  3. Are the curly brackets correctly placed?

    import java.util.Scanner;
    
    public class RamosSLE32 { //Begin class
    
        public static void main(String[] args) { //Begin main()
    
            Scanner input = new Scanner(System.in);
            int age;
            String correctPassword = "MonthyPython";
            char tryAgain = 'Y';
            char tryAgain2 = 'Y';
            String q1 = "%nWhat is the password?";
            String q2 = "%nEnter your age: ";
    
            while (Character.toUpperCase(tryAgain) == 'Y') {
                System.out.printf(q1);
                String password = input.nextLine();
                if (!password.equals(correctPassword)) {
                    System.out.printf("%nInvalid password!%nDo you want to try again? Y or N.");
                    tryAgain = input.nextLine().charAt(0);
                }
                else {
                    if (password.equals(correctPassword)) {
                        System.out.printf(q2);
                        age = input.nextInt();
                    }
    
                    {
                        if (age > 17) {
                            System.out.printf("%nShangri-La welcomes you to your earthly paradise!%nThank you! Have a nice day!");
                        } else {
                            if (age <= 17) {
                                System.out.printf("%nSorry, NO underaged patrons allowed!%nDo you want to try again? Y or N.");
                                tryAgain2 = input.nextLine().charAt(0);
                            }
                        }
                    }
                }
            }
    
        } // End main()
    
    } // End class
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1) Your variable `age` is declared but not initialized (i.e. it does not have an initial value) - yes, it should be fixed (2) if you (the author) are not sure, then your code is not clear and would be difficult to read for others (3) proper placement of brackets will help you with (2) – blurfus Sep 30 '19 at 16:21
  • `age` is only initialized if this condition evaluates to true: `if(password.equals(correctPassword))`. Otherwise it remains uninitialized and you cannot use it for the next condition `if(age > 17)`. Also your code is really hard to follow with those kind of indentations and braces placements – QBrute Sep 30 '19 at 16:22
  • 1
    use indentation and line-breaks to make you code easier to read. That would help you removing the useless curly braces – jhamon Sep 30 '19 at 16:26
  • 1
    1) you don't need to check password 2 times - for non-equality and equality - that is function of `else`. Same for `age` if i is not `>17` it will execute `else` and there is no need to check for `<=17`. 2) it is more or less convention to write `} else if (...) {` without using an extra curly brace and indentation for the `else` (it is not wrong, but seldom used) – user85421 Sep 30 '19 at 16:35
  • Thank you to all who commented. Much appreciated. –  Sep 30 '19 at 16:50

1 Answers1

0

You are getting variable age might not have been initialized because age is local variable and local variables do not get default values.
Their initial values are undefined without assigning values by some means. Before you can use local variables they must be initialized.
For more details please refer: default-values-and-initialization-in-java

Pradeep
  • 12,309
  • 3
  • 20
  • 25