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');
doloops.