Results 1 to 2 of 2
Thread: perfecting a menu class
- 12-19-2009, 04:42 PM #1gcampton Guest
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
MenuUtils.javaJava 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); } }
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.
- 12-20-2009, 05:27 AM #2gcampton Guest
Similar Threads
-
Menu class
By Kligham in forum New To JavaReplies: 14Last Post: 09-28-2009, 06:08 PM -
Fill a menu dynamically when menu is shown
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:47 PM -
React to menu action and checkbox menu
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:50 PM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 07:54 AM -
Help with menu class
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks