0

I'm creating a simple menu-driven program (which I've done before) with a switch statement. I've declared the global variable cuser, and it works for the first case in the switch. However, if I use it in the other cases it thinks that cuser hasn't been initialized. Referencing cuser with another variable doesn't work either. It looks like it should work, but I just can't figure out why it won't.

    String cuser; //Current user of e-mail system
    String recip; //Recipient of message

    char choice; //User input 

    do
    {
        choice = menuScan.next().charAt(0);

        do
        {   
            switch(choice)
            {
                case 'I':
                case 'i':
                    System.out.println("Username: ");
                    cuser = menuScan.next();
                    System.out.println("Logged in.");
                    break;

                case 'S':
                case 's':
                    System.out.println("Recipient: ");
                    recip = menuScan.next();

                    m = new Message(cuser,recip);
                    System.out.println("Enter message. Blank line to quit: ");
                    m.append(menuScan.nextLine());  

                    ms.deliver(m);
                    break;

                case 'R':
                case 'r':
                    ms.printMessages(cuser);
                    break;

                case 'O':
                case 'o':
                    System.out.println("Logged out.");
                    break;

                //No 'Q' case; serves no purpose other than termination

                default:
                    System.out.println("Invalid selection.");
                    break;
            }   
        }
        while(choice != 'O' && choice != 'o');
    }
    while(choice != 'Q' && choice !='q');
2
  • Aside from the problem identified in the answers, it's also unclear to me why you have nested do loops. Commented Mar 20, 2016 at 0:38
  • Side note: you can use toLowerCase() on your input; then you only need to check for ONE character. And don't put a switch in a loop in a loop. Commented Mar 20, 2016 at 0:44

3 Answers 3

1

Change String cuser; to String cuser = null; or String cuser = "";

The Java compiler is detecting that it's possible1 for cuser to not have been initialized after your first switch.

1Which may, or may not, be true. The compiler isn't sentient, and it can't determine all possible code paths.

Sign up to request clarification or add additional context in comments.

Comments

0

What happens if the first input to your program is s? It would reference cuser without having executed an assignment for cuser first! The compiler won't let you do that because local variables are (logically) empty before they have been assigned. In other words, the compiler is right: there is some code path that reaches a reference to cuser without going through an assignment to cuser first.

Fortunately, the fix is really simple: make sure you have written to cuser before you try to read from it! The easiest way to do that would probably be to initialize cuser when you declare it. You can write any value to it that makes sense in the logic of your program; you just need to write some value to it before the compiler will let you read a value from it. For example:

String cuser=null;

would work just fine. With that done, there will be no code path that will read from cuser without having written to it first because you write to cuser first thing! Again, any fix that guarantees each read is preceded by some write would work fine; initialization is just one solution, and happens to be the simplest.

Comments

0

You don't get the warning in the first case because you are not trying to use the value in that variable, instead you are actually initializing it. In the second case you are trying to use it and it has not been initialized yet, hence the warning.

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.