Search code examples
javafunctionreturn

Input Menu Screen Method always Returns 0


I was assigned to create a menu driven program which contained a function that showed the menu screen/get the user input, but displaymenu() always and only returns 0 instead of the new inputted number.

I'm pretty new to programming and I've googled everything I can think of and I'm pretty stuck.

import java.util.Scanner;

public class menuDriven
{
   public static void main(String[] args)
   {
      int menuSelection=0;
      double inches, centimeters, feet, meters, miles, kilometers;

      Scanner keyboard = new Scanner(System.in);

      displayMenu(keyboard, menuSelection);


      switch(menuSelection)
      {
      case 1:
         inchesToCentimeters(keyboard);
         break;

      case 2:
         feetToMeters(keyboard);
         break;

      case 3:
         milesToKilometers(keyboard);
         break;
      }

   } // end main

   public static int displayMenu(Scanner keyboard,  int menuSelection) 
   {

   System.out.println("1. Convert inches to centimeters");
   System.out.println("2. Convert feet to meters");
   System.out.println("3. Convert miles to kilometers");
   System.out.println();

   System.out.println("Enter your selection");
   menuSelection = keyboard.nextInt();
    while (menuSelection < 1 || menuSelection > 3)
      {
         System.out.println("Please enter 1, 2, or 3.");
         menuSelection = keyboard.nextInt();
      }
   return menuSelection;
   }

   public static void inchesToCentimeters(Scanner keyboard)
   {
      double inches, centimeters;
      System.out.print("Enter the number of inches: ");
      inches = keyboard.nextDouble();
      centimeters = inches * 2.54;
      System.out.println("That is equal to " + centimeters + " 
centimeters.");

   }

   public static void feetToMeters(Scanner keyboard)
   {
      double feet, meters;
      System.out.println("Enter the number of feet: ");
      feet = keyboard.nextDouble();
      meters = feet * 0.3048;
      System.out.println("That is equal to " + meters + " meters.");
   }

   public static void milesToKilometers(Scanner keyboard)
   {
      double miles, kilometers;
      System.out.println("Enter the number of miles: ");
      miles = keyboard.nextDouble();
      kilometers = miles * 1.609;
      System.out.println("That is equal to " + kilometers + " 
kilometers.");
   }
} // end class

The program should take the user input 1, 2, or 3 and then take that number to the switch and display the code.


Solution

  • Java passes primitive arguments by value, so although you manipulate the menuSelection parameter in displayMenu, menuSelection in main stays the same as it does before calling displayMenu.

    You could assign the result of displayMenu to menuSelection:

    menuSelection = displayMenu(keyboard, menuSelection);
    

    You can also remove the passing of menuSelection as a parameter & have it declared inside displayMenu, eg:

    public static int displayMenu(Scanner keyboard) {
        int menuSelection=0;
    
        //...
    }