943,147 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 1511
  • Java RSS
-1

Menu and Input Confirmation

by on Feb 27th, 2010
This program creates 2 methods for convenience: menu() and binaryQ(). The menu method accepts several strings as its parameters. The first of the strings is the question to be displayed at the top of the menu. The following strings are the menu choices. For instance, if you call Confirmer.menu("What is your favorite fruit?","Apple","Orange","Banana"); the program will print the following:

What is your favorite fruit?
1) Apple
2) Orange
3) Banana
Enter selection number:

The binaryQ method accepts a single string as a parameter, representing the question. Then it asks the user to answer yes or no. Capitalization of the answer will not affect the code. If the user answers with a word that begins with y or n, the computer will read it as yes or no -- this way, "yea", "yup" and "yes" are all evaluated the same way. Of course, if you answer "Yetti," the computer will still evaluate that as an affirmative answer. If the answer is yes, the method returns true; if the answer is no the method returns false.

Neither method will crash from invalid input -- instead they will simply request valid input, and will loop until valid input is provided.
Last edited by kvass; Feb 27th, 2010 at 4:02 am.
Java Code Snippet (Toggle Plain Text)
  1. import java.util.Scanner;
  2. /**
  3.  * This program is used to allow choice confirmation in a robust way.
  4.  * All the methods in this class are static.
  5.  *
  6.  * @author Kvass
  7.  * @version 1.0
  8.  */
  9. public final class Confirmer
  10. {
  11. /**
  12.   * Prints out a menu of option choices and requires the user to select
  13.   * a valid option choice.
  14.   * @param message The prompt to display above the menu
  15.   * @param args The menu choices
  16.   * @return The option number from the menu
  17.   */
  18. public static int menu(String message, String... args)
  19. {
  20. Scanner input = new Scanner(System.in);
  21. int choice = -1;
  22. boolean exit = false;
  23. do
  24. {
  25. try
  26. {
  27. System.out.println(message);
  28. int counter = 1;
  29. for (String s:args)
  30. {
  31. System.out.println(counter+") "+s);
  32. counter++;
  33. }
  34. System.out.print("Enter selection number: ");
  35. String temp = input.nextLine();
  36. choice = Integer.parseInt(temp);
  37. if (choice>0 && choice<counter)
  38. exit = true;
  39. else
  40. System.out.println("That number was not an option.");
  41. }
  42. catch (Exception e)
  43. {
  44. System.out.println("Error: Invalid input.");
  45. }
  46. }
  47. while (!exit);
  48. return choice;
  49. }
  50.  
  51. /**
  52.   * Displays a message and requires the user to answer yes or no.
  53.   * @param message The prompt to display above the menu
  54.   * @return true if the answer is yes, false if the answer is no.
  55.   */
  56. public static boolean binaryQ(String message)
  57. {
  58. Scanner input = new Scanner(System.in);
  59. boolean choice = false;
  60. boolean exit = false;
  61. do
  62. {
  63. try
  64. {
  65. System.out.print(message);
  66. String temp = input.nextLine();
  67. if (temp.charAt(0) == 'y' || temp.charAt(0) == 'Y')
  68. {
  69. choice = true;
  70. exit = true;
  71. }
  72. else if (temp.charAt(0) == 'n' || temp.charAt(0) == 'N')
  73. {
  74. choice = false;
  75. exit = true;
  76. }
  77. else
  78. System.out.println("It's a yes or no question.");
  79. }
  80. catch (Exception e)
  81. {
  82. System.out.println("Error: Invalid input.");
  83. }
  84. }
  85. while (!exit);
  86. return choice;
  87. }
  88.  
  89. /**
  90.   * Default constructor: private so this object cannot be constructed
  91.   */
  92. private Confirmer(){}
  93. }
Comments on this Code Snippet
Feb 27th, 2010
0

Re: Menu and Input Confirmation

Um... someone voted this code down (-1 next to code snippet)? why? Is something wrong with it?
Junior Poster
kvass is offline Offline
165 posts
since Aug 2009
Feb 27th, 2010
0

Re: Menu and Input Confirmation

Is this a question or code snipped?
Code tags enforcer
peter_budo is offline Offline
6,649 posts
since Dec 2004
Feb 27th, 2010
0

Re: Menu and Input Confirmation

Snippet -- the code works. It is for ppl to use.
Junior Poster
kvass is offline Offline
165 posts
since Aug 2009
Feb 27th, 2010
0

Re: Menu and Input Confirmation

You are missing obvious there, main method to call it program...
Code tags enforcer
peter_budo is offline Offline
6,649 posts
since Dec 2004
Feb 27th, 2010
0

Re: Menu and Input Confirmation

...This isn't supposed to be a program that performs a set task. You are supposed to use it in your own program. The Math class does not have a main method -- it simply serves as a final class with several static methods that can be called. This class acts the same way -- it is for programmers here to use if they want to.
Junior Poster
kvass is offline Offline
165 posts
since Aug 2009
Feb 28th, 2010
0

Re: Menu and Input Confirmation

Ok now I have a -2 on this snippet... can someone please tell me what people don't like about the code? Because I think it works fine, but it keeps getting votes off. There isn't supposed to be a main method -- it is just supposed to be a tool for people to use like the Math class.
Junior Poster
kvass is offline Offline
165 posts
since Aug 2009
Mar 1st, 2010
0

Re: Menu and Input Confirmation

Don't worry about the down voting. It is a system recently implemented in order to improve the forum.

But many people miss use it:
You will find entire threads of people complaining about how their perfectly good posts got down voted for no reason whatsoever by persons unknown.
I mean, I had a correct post that was written many months before they added that "voting system" and some days after the "Voting system" was added to this forum; I got down voted; my many months old thread!

So expect this to happen. It has happened to other posters that are much better than you and me
Nearly a Senior Poster
javaAddict is offline Offline
3,256 posts
since Dec 2007
Mar 1st, 2010
0

Re: Menu and Input Confirmation

It's respectable that you posted a code snippet, despite the many reasons one could find not to do so. So respect to you for that. And I typically just up vote or leave a reputation comment (which auto up votes) if I see down votes that appear to be for no reason.
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 1st, 2010
0

Re: Menu and Input Confirmation

xD I'm not like concerned "omg they downvoted me" as much as I am concerned "omg that means something is wrong with my code that I didn't catch!" ^^ but if that's not the case idc
Junior Poster
kvass is offline Offline
165 posts
since Aug 2009
Message:
Previous Thread in Java Forum Timeline: my program is compiled.but on run that i have message "Exception in thread "main" jav
Next Thread in Java Forum Timeline: StringTokenizer problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC