Riad
Riad

Reputation: 25

Variable 'menu' might not have been initialized

I tried to make a menu that uses switch and do while, but it returns:

variable 'menu' might not have been initialized

} while (menu != 5);

Can anybody help me please? This is my goal:

pic

import java.util.Scanner;
  class HW {
    public static void main(String[] args) {
    Scanner entry = new Scanner(System.in);
    int A, B, X, menu; // there
    do {
      System.out.println("Main Menu");
      System.out.println("1.  Plus");
      System.out.println("2.  Minus");
      System.out.println("3.  Multiply");
      System.out.println("4.  Division");
      System.out.println("5.  Exit");
      System.out.println("Option :");
      X = entry.nextInt();
      switch (X) {
        case 1:
          System.out.println("Var A : ");
          A = entry.nextInt();
          System.out.println("Var B : ");
          B = entry.nextInt();
          System.out.println("Result : " + (A + B));
          if (A > B)
            System.out.println(A + ">" + B);
          else if (A == B)
            System.out.println(A + "=" + B);
          else if (A < B)
            System.out.println(A + "<" + B);
          System.out.println("*press any key back to menu*");
          menu = entry.nextInt(); // there
          break;
        case 2:
          System.out.println("Var A : ");
          A = entry.nextInt();
          System.out.println("Var B : ");
          B = entry.nextInt();
          System.out.println("Result : " + (A - B));
          if (A > B)
            System.out.println(A + ">" + B);
          else if (A == B)
            System.out.println(A + "=" + B);
          else if (A < B)
            System.out.println(A + "<" + B);
          System.out.println("*press any key back to menu*");
          menu = entry.nextInt(); //there
          break;
        case 3:
          System.out.println("Var A : ");
          A = entry.nextInt();
          System.out.println("Var B : ");
          B = entry.nextInt();
          System.out.println("Result : " + (A * B));
          if (A > B)
            System.out.println(A + ">" + B);
          else if (A == B)
            System.out.println(A + "=" + B);
          else if (A < B)
            System.out.println(A + "<" + B);
          System.out.println("*press any key back to menu*");
          menu = entry.nextInt(); //there
          break;
        case 4:
          System.out.println("Var A : ");
          A = entry.nextInt();
          System.out.println("Var B : ");
          B = entry.nextInt();
          System.out.println("Result : " + (A / B));
          if (A > B)
            System.out.println(A + ">" + B);
          else if (A == B)
            System.out.println(A + "=" + B);
          else if (A < B)
            System.out.println(A + "<" + B);
          System.out.println("*press any key back to menu*");
          menu = entry.nextInt(); //there
          break;
        case 5:
          break;
      }
    } while (menu != 5); //here it say not initialized.
    System.out.println(" ");
  }
}

Upvotes: 0

Views: 465

Answers (4)

Suresh Atta
Suresh Atta

Reputation: 121998

while (menu != 5);

See, what if no cases satisfied?
That is why you need an initialization there.

Either you need to initialize or provide a default case in switch.

So, that's why there is always guaranteed to initialize.

Upvotes: 1

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6758

You have only declared the variables int A, B, X, menu; but not initialized these. So, what you have to do is put any value according to your requirement to manu variable.

Upvotes: 0

spork
spork

Reputation: 1265

If your only goal is to make the error go away, initialize the menu variable:

int A;
int B;
int X;
int menu = -1; // Initialize menu to -1

Your switch statement needs to be sure to handle all possible cases, though. You can ensure it does by adding a default case.

Upvotes: 0

Wyatt Lowery
Wyatt Lowery

Reputation: 513

You need to create a default statement after your last case. This is because if none of the cases are true, it still needs a "back-up plan". It's the counter part for the else statement.

Add this after case: 5:

default:
    menu = desiredNumberHere;
    break;

Upvotes: 1

Related Questions