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