Results 1 to 2 of 2
  1. #1
    gcampton Guest

    Default perfecting a menu class

    So I have perfected in my mind a couple of menu utilities I have created. But there's one that I am wanting to have the same output as others, but having difficulty coding. The bottom method cMenu in MenuUtils.
    Does anyone have any ideas how I can validate the input much like I have done with iMenu?
    Also was wondering if I should make these static?

    MenuTest.java
    Java Code:
    public class MenuTest
    {
       public static void main(String[] args)
       {
          String[] test = new String[]{ "pizza","pie","beer","scotch" };
          String myMenu="This is a cute Menu";
          int choice;
    
          MenuUtils mu = new MenuUtils();
    
          choice = mu.iMenu(myMenu,test);
    
       }
    }
    MenuUtils.java
    Java Code:
    import java.util.Scanner;
    
    public class MenuUtils
    {
       // prints menu
       public void printMenu(String menuText, String[] menuOptions)
       {
          // print title
          System.out.printf("*****%s*****\n", menuText);
    
          // print options
          for ( int i=0; i<menuOptions.length; i++ )
             System.out.printf("**\t%d)  %s\n", i+1, menuOptions[i]);
    
          // print number of stars
          for ( int j=0; j<menuText.length(); j++ )
             System.out.printf("*");
          System.out.printf("**********\n");
          System.out.printf("->: ");
       }
    
       // validate options
       public int iMenu ( String menuText, String[] menuOptions )
       { 
          Scanner input = new Scanner(System.in);
          int choice;
          printMenu ( menuText, menuOptions );
          do
          {
             do
             {
                if (!(input.hasNextInt()))
                {
                   System.out.printf("Invalid OPTION, Please enter number in menu: ");
                   input.nextLine();
                }
             }
             while (!( input.hasNextInt())); // do while not int
             choice = input.nextInt();
    
             if ((choice<1) || (choice>menuOptions.length))
             {
                System.out.printf("invalid NUMBER, Please enter a number in menu: ");
                input.nextLine();
             }
          }
          while ((choice<1) || (choice>menuOptions.length)); // do while not in range
          return choice;
       }
    
       // same as above menu only using char
       public char cMenu ( String menuText, String[] menuOptions )
       {
          Scanner input = new Scanner(System.in);
          char choice;
          
          printMenu(menuText, menuOptions);
          choice = (input.next().charAt(0));
          return choice;
       }
    
    }

    output is a simple menu:
    Java Code:
    *****This is a cute Menu*****
    **   1) pizza
    **   2) pie
    **   3) beer
    **   4) scotch
    ************************
    ->:  dfgdfg
    Invalid OPTION, Please enter number in menu: dfgbfe 3w4r sd32
    Invalid OPTION, Please enter number in menu: 4591635
    Invalid NUMBER, Please enter number in menu: 3
    
    Process complete.


    EDIT: I was thinking I might be able to use: public String hasNext(Pattern pattern)
    But there's no way of telling how many menu items I might receive, really I would want to be able to temporarily convert menu choices to ints, validate, then return the given char. But I can't see how this would work.

    The logic is easy enough, While chars are not in range of menuOptions, keep asking for input.
    Last edited by gcampton; 12-19-2009 at 05:25 PM.

  2. #2
    gcampton Guest

Similar Threads

  1. Menu class
    By Kligham in forum New To Java
    Replies: 14
    Last Post: 09-28-2009, 06:08 PM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  4. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  5. Help with menu class
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •