user5455401
user5455401

Reputation: 23

Variable Errors in Menu Program

I want to create a Menu Program, with different options:

1)Enter Name
2)Display Name 
3)Change Name
4)Quit

But for some reason I can't get the variable used to store the name in Q1, to be used in Q2!

Here's the code: (The error occurs in case 2, with the variable full_name being underlined red and not working)

package Testing;
import java.util.Scanner ;
public class Menu {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    // print menu
    for (int i = 1; i <= 5; i++)
          System.out.println(i + ". Menu item #" + i);
    System.out.println("0. Quit");
    // handle user commands
    boolean quit = false;
    int menuItem;
    do {
          System.out.print("Choose menu item: ");
          menuItem = in.nextInt();
          switch (menuItem) {
          case 1:
                System.out.println("You've chosen item #1");
                {
                    Scanner user_input = new Scanner(System.in);

                    String first_name;
                    System.out.print("Enter your first name: ");
                    first_name = user_input.next( );

                    String family_name;
                    System.out.print("Enter your family name: ");
                    family_name = user_input.next( );

                    String full_name;
                    full_name = first_name + " " + family_name;

                    System.out.println("You are " + full_name);
                }

                break;
          case 2:
                System.out.println("You've chosen item #2");
                System.out.println("You are " + full_name);
                break;
          case 3:
                System.out.println("You've chosen item #3");
                // do something...
                break;
          case 4:
                System.out.println("You've chosen item #4");
                // do something...
                break;
          case 5:
                System.out.println("You've chosen item #5");
                // do something...
                break;
          case 0:
                quit = true;
                break;
          default:
                System.out.println("Invalid choice.");
          }
    } while (!quit);
    System.out.println("Bye-bye!");
 }
}

Upvotes: 1

Views: 68

Answers (2)

babaelmo
babaelmo

Reputation: 29

import java.util.Scanner ;
public class Menu {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in); 
    // print menu
    for (int i = 1; i <= 5; i++)
          System.out.println(i + ". Menu item #" + i);
    System.out.println("0. Quit");
    // handle user commands
    //The name
    String first_name = "";
    String family_name = "";
    String full_name = "";
    while(true) {
          System.out.print("Choose menu item: ");
          int menuItem = 0;
          try { // If you enter a string it will throw an exception!
              menuItem = in.nextInt();
          } catch(Exception e) { 
              System.out.println(e.getMessage()); 
          }
          switch (menuItem) {
          case 1:
                System.out.println("You've chosen item #1");
                System.out.print("Enter your first name: ");
                first_name = in.next( );

                System.out.print("Enter your family name: ");
                family_name = in.next( );

                full_name = first_name + " " + family_name;
                System.out.println("You are " + full_name);
                break;
          case 2:
                System.out.println("You've chosen item #2");
                System.out.println("You are " + full_name);
                break;
          case 3:
                System.out.println("You've chosen item #3");
                // do something...
                break;
          case 4:
                System.out.println("You've chosen item #4");
                // do something...
                break;
          case 5:
                System.out.println("You've chosen item #5");
                // do something...
                break;
          case 0:
                System.out.println("bye-bye");
                System.exit(0); // Exit
                break;
          default:
                System.out.println("Invalid choice.");
          }
      }
   }
}

Upvotes: 0

Fatih Donmez
Fatih Donmez

Reputation: 4347

You should put full_name outside of loop, because currently it's in the local scope which is inaccessible from other case statements.

Upvotes: 1

Related Questions