I'm supposed to make a menu with choices, the choices being different kinds of java syntax.

import java.util.*;

public class Start 
{
    public void menu()
    {
    System.out.println("[1] If\n[2] If else\n[3] For loop\n[4] Switch Statement\n[5] One dimension");
    System.out.print("Select program: ");
    }
    public void backend()
    {
     System.out.println("[9] Menu\n[0] Quit");

     Scanner q = new Scanner(System.in);

     switch (q.nextInt())
       {
       case 0:
       System.out.println("End of the program.");
       break;

       case 9:
       new Input();
       break;

       default:
       System.err.println("Please choose the appropriate options.");
       break;

       }
     }
     public void ifex()
     {

        System.out.println("[6] Gender");
        Scanner pb = new Scanner(System.in);
        switch(pb.nextInt())
       {

           case 6:
           gender();
           break;
       }
     }
     public void ifelse()
     {
         System.out.println("[10] Legal Age");
         Scanner gg = new Scanner(System.in);
         switch(gg.nextInt())
        {
          case 10:   
          break;
        } 
      }

    public void gender()
      {
       System.out.println("Choose your gender\n[7] Male\n[8] Female");
       Scanner gb = new Scanner(System.in);
       switch(gb.nextInt())
        {
         case 7:
         System.out.println("Your gender is Male");
         new backend();
         break;

         case 8:
         System.out.println("Your gender is Female");
         new backend();
         break;

         default:
         System.err.println("That is not on the options. Please try again.");
         break;
        }
       }   

         public void age()
        {
        System.out.println("Enter your age");
        Scanner gb = new Scanner(System.in);
        Int age = input.nextInt();

          if (age < 12);
          System.out.println("You are not yet in a legal age.");

          System.out.println("You are in a legal age");
          backend();
          break;

       }

  }          

  public Start()
  {
  Scanner in = new Scanner(System.in);
  menu();

    switch (in.nextInt())
    {
    case 1: 
    ifex();
    break;

    case 2:
    ifelse();
    break;
    }

}

  public static void main (String[]args)
  {
     new Start();
  }
}

Recommended Answers

Its a very bad idea to have multiple Scanners usig the same input stream (System.in). You never know which Scanner will read the next character from the stream, or which Scanners may have read ahead and thus consumed characters that you hoped to read with a different Scanner.
Create just …

Jump to Post

All 2 Replies

Its a very bad idea to have multiple Scanners usig the same input stream (System.in). You never know which Scanner will read the next character from the stream, or which Scanners may have read ahead and thus consumed characters that you hoped to read with a different Scanner.
Create just one Scanner at the start of the program and use that for everything.

Your bracketing/indentaton style is confusing (although perfectly legal).

Be a part of the DaniWeb community

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