0

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();
        }
    }
}
4
  • Please indent your code properly, that's a pain to read. Commented May 3, 2014 at 19:43
  • i dont see how its difficult to read, but ok. Commented May 3, 2014 at 19:46
  • 2
    @user3599905: Is that how you've got it indented in your original source code too? It's always a good idea to indent your source code, and most IDEs will do that for you. Then make sure that if you post a question on Stack Overflow, you maintain the same indentation in the question as you'd have in your IDE. Commented May 3, 2014 at 19:49
  • well i indented it exactly the same as my books pseudo code indents it. text pad also has some auto indenting. Commented May 3, 2014 at 20:04

4 Answers 4

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

how would i pass selection by reference in the displayMenu module in java? example displayMenu(int ref selection)?
@user3599905: You just don't. Java doesn't have pass-by-reference, it only supports pass-by-value. Note that in the case of objects, what you're passing is a reference, but that's still passed by value. Even if you could use pass-by-reference, it would still be clearer just to use a return value.
0

Java doesn't support "true" pass-by-reference, which is what your program seems to rely on. You initialize a variable to 0 and ask a method to change it, which can't happen. The parameter values you have inside methods are mere copies of the original values passed to them. A small demonstration: consider you have this method...

public static void increment(int a) {
    a++;
}

and a code that uses it...

int a = 0;
increment(a);
System.out.println(a);

0 is printed, because the only thing that's incremented is the local copy of a in the method increment. The original value stays unchanged.

You could make your method instead return a value as suggested in Jon Skeet's answer.

Comments

0

the simplest solution when you stuck in a program is that use debug tools.
begug tools that exist in every compiler like eclipse or netbeans not only in this program but also in any program can help you.
then come back to your project and use this tools. you can find your wrong.
good luck!!

Comments

0

You call displayMenu() within the do-while loop and there is a problem with passing values between methods. And yes there is problem with your indentation. Check topics like control statements, variables in a book. An improved version is shown below:

package imporvedtranslator;

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

public class ImporvedTranslator
  {
  public static void main(String[] args)
    {
      int selection;
      selection = displayMenu();

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

  private  static int displayMenu()
    {
      int sel;
      Scanner sc = new Scanner(System.in);

      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");

      sel = sc.nextInt();

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

      return sel;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.