Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem while implementing a basic user interface menu

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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-
    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 -
    					System.out.print("\nPlease enter a fully parenthesized expression: ");
    					Paren = kb.nextLine();
    					kb.nextLine();
    					fullParen = new ParenExpression(Paren);
    Any tips would be appreciated


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Problem implementing a user interface using a menu

    Hello Rastabot and welcome to the Java Programming Forums

    What is ParenExpression?

    I can't compile the above code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by Rastabot; April 3rd, 2009 at 03:26 PM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Problem implementing a user interface using a menu

    Quote Originally Posted by Rastabot View Post
    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM