-3

i am getting a "variable selection may not have been initialized in displayMenu(selection). and im not sure why. is it not initialized in the displayMenu model or am i missing something? does "selection = keyboard.nextInt" not count as an initialization? im kind of confused at why i am getting this error. here is the code:

import java.util.Scanner;
import java.io.*;

public class LanguageTranslatorIB
{
    public static void main(String[] args)
    {
// local variable to hold the menu selection
int selection;

do
{

    // display the menu
    displayMenu(selection);

    // perform the selected operation
        switch (selection)
        {
        case 1:
        System.out.println("Good Morning.");

        case 2:
        System.out.println("Buongiorno.");

        case 3:
        System.out.println("Buenos dias.");

        case 4:
        System.out.println("Guten morgen.");

        case 5:
        System.out.println("GoodBye!");

        }
    }
        while (selection != 5);
    }





// the displayMenu module displays the menu and gets and validates
// the users selection.

    public static void displayMenu(int selection)
    {
        //keyboard scanner
        Scanner keyboard = new Scanner(System.in);

    // display the menu
    System.out.println(" select a language and i will say good morning");
    System.out.println("1. English.");
    System.out.println("2. Italian.");
    System.out.println("3. Spanish.");
    System.out.println("4. German.");
    System.out.println("5. End the Program.");
    System.out.println("Enter your selection");

    // users selection
    selection = keyboard.nextInt();

    while (selection < 1 || selection > 5)
    {
        System.out.println ("that is an invalid select.");
        System.out.println (" Enter 1, 2, 3, 4, or 5.");
        selection = keyboard.nextInt();
    }

    }
}

6 Answers6

2

When you pass the variable selection into displayMenu the original variable does not change. The variable that is changed inside that method is a copy. Anything you do inside that method has absolutely no effect on the original selection

Therefore selection has not been initialized as is correctly pointed out by the compiler

You need to redesign the displayMenu to return a value which will be assigned to selection. No input to that method is needed

On another note, you probably want to add break after each System.out.println inside the case statements. If you don't the control will fall through to each next case.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • i got passed the error and added breaks and it compiles, but im getting a infinite loop with the menu. its displaying the selection menu over and over again without displaying the appropriate translation for the selection. everything else works fine tho – user3530421 May 03 '14 at 19:05
  • That is a different issue. You should try to work on it and if you can't get it to work, ask for some help in a different SO question in order not to confuse readers – geoand May 03 '14 at 20:33
1

Change displayMenu not take a parameter but rather to return the selected int. Assign that to selection:

selection = displayMenu();

and...

public static int displayMenu()
{
   int selection = 0;
   Scanner keyboard = new Scanner(System.in);
   // ....

   selection = keyboard.nextInt();

   while (selection < 1 || selection > 5)
   {
      //...
   }
   return selection;

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Your primitive variables are passed to methods as copy, not reference. So when you're calling

displayMenu(selection);

this method is only working with the copy within the method, not the variable out of it itself.

CrociDB
  • 1,234
  • 1
  • 7
  • 20
0

in Java local/block variables need to be explicitly initialized at declaration. un-initialized local variables give this error. Ifyou assume that the selection local variable will be given the default value that an int has in Java then you are wrong. The same is not the case with class level variables, the ones which you define in your class as properties. These variables are assigned their default values automatically by compiler.

Since you havent assigned any value to 'selection' before using in later on so you get this error.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
0

In your main method, selection does not store any value at any point. You need to setup displayMenu to return an integer to selection

Jonathon Anderson
  • 1,162
  • 1
  • 8
  • 24
0

When you call displayMenu(selection); java passes the value of the selection variable to the displayMenu() method. The variable hasnt been initialized yet.

Then you're trying to set the value of selection variable inside the displayMenu() method.

However, the selection variable that you have as a parameter of the displayMenu() is local to that method and even though the value of the local selection variable is set, the selection variable inside the main method still remains uninitialized.

To tackle this : Create an instance variable.

public class LanguageTranslatorIB
{
    int selection;
    public static void main(String[] args)
    {
        displayMenu();
        //Rest of the code follows;
    }
}


public static void displayMenu()
{
    //keyboard scanner
    Scanner keyboard = new Scanner(System.in);

    // display the menu
    System.out.println(" select a language and i will say good morning");
    System.out.println("1. English.");
    System.out.println("2. Italian.");
    System.out.println("3. Spanish.");
    System.out.println("4. German.");
    System.out.println("5. End the Program.");
    System.out.println("Enter your selection");

    // users selection
    selection = keyboard.nextInt();

    while (selection < 1 || selection > 5)
    {
        System.out.println ("that is an invalid select.");
        System.out.println (" Enter 1, 2, 3, 4, or 5.");
        selection = keyboard.nextInt();
    }

    }
}
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34