Hey, I need to create a basic user interface menu. My code complies, however it doesnt seem to work properly. Heres the code-
I think the problem might come from the flushing in the first response around lines 46-49, however I'm not sure. Those lines of code are -import java.util.*; public class Program3 { public static void main(String[ ] args) { //declare variables String Paren = ""; ParenExpression fullParen = new ParenExpression(Paren); Scanner kb = new Scanner(System.in); int response; //do while loop that controls program System.out.println("Welcome to the Program3"); do { //show the menu System.out.println("\n\nChoose an option"); System.out.println("\t1\tCreate a new fully parenthesized expression"); System.out.println("\t2\tPrint the expression"); System.out.println("\t3\tPrint what it evaluates to"); System.out.println("\t4\tPrint the postfix equivalent"); System.out.println("\t5\tQuit"); //get the user response to menu try { response = kb.nextInt(); //get it } catch(InputMismatchException e) { response = -1; //something illegal kb.nextLine(); //flush it } //do a logic "branch" depending on their response try { if (response == 1) { //prompt user System.out.print("\nPlease enter a fully parenthesized expression: "); Paren = kb.nextLine(); kb.nextLine(); fullParen = new ParenExpression(Paren); } else if (response == 2) { System.out.println(fullParen); } else if (response == 3) { System.out.println(fullParen.evaluate()); } else if (response == 4) { System.out.println(fullParen.asPostfix()); } else System.out.println("\nInvalid input - try again"); } catch (Throwable theException) //...catches everything (since every one is a subclass of Throwable) { System.out.println("got something - " + theException); theException.printStackTrace(); //prints the calling sequence up to the exception } } while (response != 5); } }
Any tips would be appreciatedSystem.out.print("\nPlease enter a fully parenthesized expression: "); Paren = kb.nextLine(); kb.nextLine(); fullParen = new ParenExpression(Paren);