Java Menu going into an infinite loop

i'm getting an infinite loop with the program i'm doing and i'm not sure how to get passed it. been working at this a few hours and i cant figure it out. (sorry if this seems very simple to you guys, but i'm still new at java). basically what is happening is that everything compiles and works as it should at first, but as soon as i enter a selection it repeats the main menu WITHOUT giving me the intended operation.

for example, the program should work to where i enter "1, 2, 3, or 4" as a selection and it displays "Good morning" in English, Italian, Spanish, and German respectively and 5 ends the program. what happens instead is that the menu repeats itself if i enter 1, 2, 3, 4, or 5 as a selection.

entering any number other than 1-5 displays an error message saying that the selection is invalid, as it should. so that while loop works as intended. so can anyone point out what i did wrong?

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

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

        do
        {
            // display the menu
            displayMenu(selection);

            // perform the selected operation
            switch (selection)
            {
            case 1:
                System.out.println("Good Morning.");
                break;
            case 2:
                System.out.println("Buongiorno.");
                break;
            case 3:
                System.out.println("Buenos dias.");
                break;
            case 4:
                System.out.println("Guten morgen.");
                break;
            case 5:
                System.out.println("GoodBye!");
            break;
            }
        } 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();
        }
    }
}
Jon Skeet
people
quotationmark

The problem is that arguments to methods are passed by value in Java. So the part at the end of displayMenu where you set a new value for the selection parameter doesn't change the value of the selection local variable in main at all. So in main, the selection variable always has the value of 0.

Given that you're not using the original value of selection in displayMenu, you should just turn it into a method with no parameters, but which returns the selected value:

public static int displayMenu()
{
    // Code to print out the menu [...]

    int 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();
    }
    return selection;
}

And in main:

int selection;
do
{
    selection = displayMenu();
    switch (selection)
    {
        ...
    }
}
while (selection != 5);

people

See more on this question at Stackoverflow