creating a simple console menu
Hello everyone,
I have to create a simple menu with options
A. Do something and return to the menu once complete
B. Do something and return to the menu once complete
C. Exit program
So when user enters A it executes the first process then returns to the menu etc.
I tried to use a switch but I could only make it work with int not with char. (eg. clicking int 1 runs option 1 but how do I make it clicking char A instead?)
Now I'm trying if/ese
import java.util.*;
// Display a menu with the option to check for prime numbers and check for vowels or exit.
public class testing
{
public static void main(String args[])
{
//Display menu options
System.out.println("*************** Menu *************** ");
System.out.println("A. Check to see if a number is prime. ");
System.out.println("B. Count the number of vowels in a line. ");
System.out.println("X. Exit the program. ");
System.out.println("****************************** ******************* ");
Scanner sc = new Scanner(System.in);
System.out.println("Please select one of the above options:");
String inputChar = sc.nextLine();
So after this what do I use to scan for the input char?
is it
if inputChar == 'A || a'
do
else ?
Or
if (options = 'a' || 'A')
do
else
if (options = 'b' || 'B')
do
etc
I'm sorry I'm really new at this and struggling big time and because I'm doing my degree long distance I don't have much help.
I just need a push in the right direction.
Re: creating a simple console menu
Hello Jackiep,
I would suggest sticking with the switch statement, in this case it would just be nice due to the ability to use the default case. What you need to remember is that nextLine() returns a String and not a char so you would have to be careful to use quotation marks instead of apostrophes. (i.e. "a" and not 'a') Also, there is a nice toLowerCase() method for strings which could come in handy.
- Michael
Re: creating a simple console menu
Quote:
Originally Posted by
KucerakJM
Hello Jackiep,
I would suggest sticking with the switch statement, in this case it would just be nice due to the ability to use the default case. What you need to remember is that nextLine() returns a String and not a char so you would have to be careful to use quotation marks instead of apostrophes. (i.e. "a" and not 'a') Also, there is a nice toLowerCase() method for strings which could come in handy.
- Michael
Thanks for the advice.
I wanted to use a switch statement but when using it only "case 1" seems to work. I can't get "case A" to work. I need the input to be a char not an int.
Anyway I have it working with if/else, but one problem.
How do I make the program loop back to the menu after a process has been run.
The user selects option A, that program is run, then it finishes and it needs to go back to the main menu.
Re: creating a simple console menu
I'm glad you have the if-else working out. Repeating the procedure will require a loop, a while loop would probably suffice. For future reference the switch would work like this:
Code Java:
Scanner sc = new Scanner(System.in);
System.out.println("Type in a letter:");
String input = sc.nextLine();
switch(input.toLowerCase()) //This way it doesn't matter if they typed an uppercase letter
{
case "x": System.out.println("You have selected option X"); break;
case "y": System.out.println("You have selected option Y"); break;
case "z": System.out.println("You have selected option Z"); break;
default: System.out.println("You didn't enter X, Y, or Z"); break;
}