Im trying to make it so that my menu options that i've highlighted are their own method but i was having issues with returning them so i could use the selection as an int for my switch statement.Any ideas?

import javax.swing.JOptionPane;


public class ConversionProgram {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String input;
		String selection;
		int choice =0;
		int meters;
		
		
		input=JOptionPane.showInputDialog("Enter the distance in meters : ");
		meters=Integer.parseInt(input);
		
		
		
		while (choice != 4){
			
			
		[U]selection = JOptionPane.showInputDialog("1. Convert to kilometers \n 2. Convert to inches" +
					"\n" + "3. Convert to feet" + "\n" + "4. Quit the program");
		choice = Integer.parseInt(selection);[/U]
			
			
		
			
			switch(choice){
				case '1' :
					showKilometers(meters);
					break;
				case '2' :
					showInches(meters);
					break;
				case '3' :
					showFeet(meters);
					break;
				case '4' :
					System.exit(0);
				default:  JOptionPane.showMessageDialog(null, "Invalid number");
			}
		}
	}
			
			
	



		// Converts meters into feet	
			
	public static void showFeet(int meters) {
		
		double feet = meters*3.281;
		JOptionPane.showMessageDialog(null, feet);
	}

	public static void showInches(int meters) {
		// TODO Auto-generated method stub
		double inches = meters*39.37;
		JOptionPane.showMessageDialog(null, inches);
	}

	public static void showKilometers(int meters) {
		double kilometers = meters*.001;
		JOptionPane.showMessageDialog(null, kilometers);
		
	
		// TODO Auto-generated method stub
		
	}

}

Edited 6 Years Ago by toferdagofer: n/a

The data type of choice is int. So you should define each case by a constant of type int. Therefore the case bady should be:

case 1:
	howKilometers(meters);
	break;
case 2 :
	showInches(meters);
	break;
case 3 :
	showFeet(meters);
	break;
case 4 :
	System.exit(0);
	default:  JOptionPane.showMessageDialog(null, "Invalid number");

i dont understand. How does that help me get my menu to be in its own method and still be able to read the users choice into the switch statement

do you mean something like:

public int askUserForChoice() {
  // do all the stuff with the dialog box as above
  return choice;
}
...
int chosenOption = askUserForChoice();
switch (chosenOption);
// etc

do you mean something like:

public int askUserForChoice() {
  // do all the stuff with the dialog box as above
  return choice;
}
...
int chosenOption = askUserForChoice();
switch (chosenOption);
// etc

yes but do i have to do something to link the return choice with chosenOption or is that automatically done when i run the askUserForChoice method. What i mean to say is when the user pickes something is the part where it says return choice put in for askUserForChoice() part of the int chosenOption = askUserForChoice?

The method is declared as having a return type of int. This means
1. There must be a return ...; statement in the method where ... is an int
2. When you call the method it is executed and the returned int value is then used for whatever the calling statement does next, so
int chosenOption = askUserForChoice();
says call the method, and use the value it returns to set my variable chosenOption.
There's nothing else you need to do (or can do) to "link" the two.

This question has already been answered. Start a new discussion instead.