Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: creating a simple console menu

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by jackiep; March 26th, 2012 at 12:58 AM.


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    109
    My Mood
    Inspired
    Thanks
    1
    Thanked 32 Times in 30 Posts

    Default 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

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    jackiep (March 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: creating a simple console menu

    Quote Originally Posted by KucerakJM View Post
    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.

  5. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    109
    My Mood
    Inspired
    Thanks
    1
    Thanked 32 Times in 30 Posts

    Default 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:
    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;
    	}
    Last edited by KucerakJM; March 26th, 2012 at 02:08 AM.

Similar Threads

  1. Need help with creating simple calculater
    By YourCrazyFriend in forum Java Theory & Questions
    Replies: 15
    Last Post: March 2nd, 2012, 09:19 AM
  2. A simple BOX in Console! Please Help!
    By rainexel in forum Java Theory & Questions
    Replies: 9
    Last Post: September 26th, 2011, 11:37 AM
  3. Creating simple Jtable
    By Dharmarajan in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 09:24 AM
  4. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM