Is there other way to write a Menu code, not using Switch, or creating other Method
homework : create Menu in Java code

i was wondering if it can be achieved not using switch and not creating other methods
(public static void Main)(public static void sayHi())
Thank you.

7 Years
Discussion Span
Last Post by adityats2020

Is there other way to write a Menu code, not using Switch, or creating other Method

Can you define what "Menu code" is?


menu driven console application which allows the user to select one of three options
if A. do dance
B. sing
c. exit program

I am going with creating new methods for the menu, i am a noob in programming and java
I thought it's going to be simple; here are the codes i tried to write:

import java.io.*;
 public class Assignment1 {

    public static void main(String[] args)
                  throws IOException

        BufferedReader keyboard = new BufferedReader 
                                 (new InputStreamReader(System.in));

        String A, B, X ;
        int selection;

        System.out.println("A. Dance employee");        
        System.out.println("B. Sing");
        System.out.println("X. Exit the program");

        A = keyboard.readLine();
        B = keyboard.readLine();
        X = keyboard.readLine();        

        selection = Integer.parseInt(A);

with this one it's not even finish yet,, but if i select one of the options, i can't convert a char to string or vice versa..

public class DrawMenu {

    public static void menu()
        System.out.println("1. Draw a line");
        System.out.println("2. draw a square");
        System.out.println("3. exit");          


    public static void main(String[] args)
         Scanner console = new Scanner(System.in);       
         int selection ;

         selection = console.nextInt();

         while (selection != 3);
             System.out.println("select here");
             selection = console.nextInt();  


with this one I think this is my best way to solve my assignment
I just need a light in the right direction..
Thank you,,

Edited by Reverend Jim: Fixed formatting


Please enclose your code in code tags when posting it.
To procede:
First figure out what the program will do step by step
Second write the code to a file with extension .java
Third use the javac command to compile it.
Fourth look at the errors and try to correct them
Fifth if unable to correct the errors, copy and paste the full text of the errors here with your questions.


i'm trying to get the user to make a selection

i can't make it work in char or string
it works with int

 public static String getValidLetter()

      char let;
      Scanner console = new Scanner(System.in);
      let = console.nextLine() .charAt(0) ;

      while (let < a || let < A && let > z || let > Z)
             System.out.println("Invalid entry");
             System.out.println("Try again");
             let = console.nextLine() .charAt(0) ;
      return let;

Edited by Reverend Jim: Fixed formatting


parseint will convert only numbers (0,1,2..) and not any other characters to int.
Use compareTo() method in a condition to execute corresponding code.

Edited by adityats2020: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.