Problem while implementing a basic user interface menu
Hey, I need to create a basic user interface menu. My code complies, however it doesnt seem to work properly. Heres the code-
Code :
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);
}
}
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 -
Code :
System.out.print("\nPlease enter a fully parenthesized expression: ");
Paren = kb.nextLine();
kb.nextLine();
fullParen = new ParenExpression(Paren);
Any tips would be appreciated
Re: Problem implementing a user interface using a menu
Hello Rastabot and welcome to the Java Programming Forums :D
What is ParenExpression?
I can't compile the above code.
Re: Problem implementing a user interface using a menu
Never mind I just figured it out. My flush line was after my variable line, so what I typed in wasn't going in to any variable.
Re: Problem implementing a user interface using a menu
Quote:
Originally Posted by
Rastabot
Never mind I just figured it out. My flush line was after my variable line, so what I typed in wasn't going in to any variable.
Well spotted.