I ran across this site while trying to Google search for help. I noticed this site showed up a lot of times and answered the questions I had, so I decided to join. I'm writing a program from class but I'm having some trouble. I would like to loop this program if the user enters a selection that's not on the menu. I believe I need to use a while loop with the condition (number > 6)?

Also I can't figure out how to get the output to display: " _ feet are equal to _ inches"

The parts I can't figure out how to display are highlighted in red in the statement above.

Here is my program, any input is greatly appreciated! Thank you.

import java.util.Scanner;

public class homework {
	public static void main (String [] args){
		Scanner keyboard = new Scanner(System.in);

		int value;

		// Display menu

		System.out.println("Welcome to the Distance Conversion System");
		System.out.println("               Main menu                 ");
		System.out.println("	       ----------------              ");
		System.out.println("	1- Convert from Feet to Inch.        ");
		System.out.println("	2- Convert from Inch to Feet.        ");
		System.out.println("	3- Convert from Yard to Feet.        ");
		System.out.println("	4- Convert from Feet to Yard.        ");
		System.out.println("	5- Convert from Yard to Mile.        ");
		System.out.println("	6- Convert from Mile to Yard.        ");
		System.out.println("	0- Exit                              ");
		System.out.print("Enter your choice: ");
			value = keyboard.nextInt();


switch (value){
	case 1:
	System.out.print("Enter a distance in feet to be converted to inches: ");
		value = keyboard.nextInt();
	System.out.println(" feet are equal to " + value * 12);
		break;
	case 2:
	System.out.print("Enter a distance in inches to be converted to feet: ");
		value = keyboard.nextInt();
	System.out.println("inches are equal to " + value/12);
		break;
	case 3:
	System.out.print("Enter a distance in yards to be converted to feet: ");
		value = keyboard.nextInt();
	System.out.print(" yards are equal to " + value * 3);
		break;
	case 4:
	System.out.print("Enter a distance in feet to be converted to yards: ");
		value = keyboard.nextInt();
	System.out.print(" feet are equal to " + (double) value/3.0);
		break;
	case 5:
	System.out.print("Enter a distance in yards to be converted to miles: ");
		value = keyboard.nextInt();
	System.out.print(" yards are equal to " + (double) value/1760.0);
		break;
	case 6:
	System.out.print("Enter a distance in miles to be converted to yards: ");
		value = keyboard.nextInt();
	System.out.print(" miles are equal to " + value * 1760 );
		break;
	case 0:
		break;

	default:
	System.out.println("Invalid selection, please try again..!");
		value = keyboard.nextInt();

		}

	}

}

The while loop should terminate when the user enters 0 for the variable value.
You need to define value before the loop starts otherwise there will be errors

value = 1;
while(value != 0)//when the user enter 0 the loop will terminate
{
  //lines 11 - 63 go inside the while loop
  
}

As for print the "_feet to inches value"

System.out.println(value + " feet to inches " + (value * 12));
Member Avatar for ztini

Oy, to "while" loop or to "do while" loop, that is the question...

While can loop 0 to n times.
Do While can loop 1 to n times.

Since you want your menu to display at least 1 time, do while is always suggested.

Perhaps, something like this:

import java.util.Scanner;

public class ConsoleMenu {
	
	private final String TAB = "     ";
	
	public ConsoleMenu() {
		displayMenu();
	}
	
	private void displayMenu() {
	
		Scanner in = new Scanner(System.in);
		int input, value;
		
		System.out.println(TAB + TAB + "  Welcome to the");
		System.out.println(TAB + " Distance Conversion System");
		System.out.println();
		System.out.println(TAB + TAB + TAB + "Main Menu");
		System.out.println(TAB + "-----------------------------");
		System.out.println(TAB + "1- Convert from Feet to Inch.");
		System.out.println(TAB + "2- Convert from Inch to Feet.");
		System.out.println(TAB + "3- Convert from Yard to Feet.");
		System.out.println(TAB + "4- Convert from Feet to Yard.");
		System.out.println(TAB + "5- Convert from Yard to Mile.");
		System.out.println(TAB + "6- Convert from Mile to Yard.");
		System.out.println(TAB + "0- Exit");
		
		
		do {
			System.out.println();
			System.out.print(TAB + ">> ");
			input = in.nextInt();
			System.out.println();
			
			switch (input) {
			
			case 1:
				System.out.print(TAB + "Feet: ");
				value = in.nextInt();
				System.out.println(TAB + "Inches: " + value * 12);
				break;
				
			case 2:
				System.out.print(TAB + "Inches: ");
				value = in.nextInt();
				System.out.println(TAB + "Feet: " + value / 12.0);
				break;
				
			// .... etc
			
			default: 
				System.out.println(TAB + "Invalid selection!");
				continue;
			}
			
			System.out.println();
			System.out.print(TAB + "Another conversion? (Y/N) ");
			
			switch (in.next().charAt(0)) {
			case 'Y':
			case 'y': input = -1; break; 
			default: input = 0; break;
			}
			
			System.out.println();
			
		} while (input > 0);
		
		if (input == 0) 
			System.out.println(TAB + "Good-Bye!");
		else
			displayMenu();
	}
	
	public static void main(String[] args) {
		new ConsoleMenu();
	}
}

Example run:

Welcome to the
      Distance Conversion System

               Main Menu
     -----------------------------
     1- Convert from Feet to Inch.
     2- Convert from Inch to Feet.
     3- Convert from Yard to Feet.
     4- Convert from Feet to Yard.
     5- Convert from Yard to Mile.
     6- Convert from Mile to Yard.
     0- Exit

     >> 1

     Feet: 3
     Inches: 36

     Another conversion? (Y/N) y

            Welcome to the
      Distance Conversion System

               Main Menu
     -----------------------------
     1- Convert from Feet to Inch.
     2- Convert from Inch to Feet.
     3- Convert from Yard to Feet.
     4- Convert from Feet to Yard.
     5- Convert from Yard to Mile.
     6- Convert from Mile to Yard.
     0- Exit

     >> 2

     Inches: 3
     Feet: 0.25

     Another conversion? (Y/N) n

     Good-Bye!

A couple of things to note:

1) Simple way to remember which loop to use (for, while, do-while):
for = known number of iterations (known)
while = 0 to n iterations (0 to unknown)
do-while = 1 to n iterations. (1 to unknown)

2) Mattox suggested doing:

System.out.println(value + " feet to inches " + (value * 12));

Not necessary.

System.out.println(value + " feet to inches " + value * 12);

This will produce the same result. The compiler knowns that "value * 12" needs to be completed first (resulting in integer). Then it looks at the left side of the "+" and sees a string, so then it knowns the final end result is a string, not arithmetic.

3) Be careful with integer division; integer / integer must equal an integer. However, if you change either side of the equation to a double, the result must be a double.

3 / 12 = 0
3 / 12.0 = 0.25
3.0 / 12 = 0.25
3.0 / 12 = 0.25

commented: Good Post +1

Thank you both for great feedback!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.