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.

Recommended Answers

All 6 Replies

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

Can you define what "Menu code" is?

Please redefine the whole question?

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("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 ;

         menu();
         selection = console.nextInt();

         while (selection != 3);
         {
             System.out.println("select here");
             menu();
             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,,

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
A
B
X

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;
   }

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.

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.