Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SJF

Thread: Need help with program

  1. #1
    jace5869 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Need help with program

    Trying to figure out what I'm doing wrong in my programming assignment. I know I could more than likely clean it up and make it much more efficient, but I'm just crazy bad at programming. So, i'll post m assignment and my code and let some more experienced coders look at it.

    Assignment:
    1. Prompt the user for two integers.
    2. Display a menu with the following options:
    1. Add numbers
    2. Subtract numbers
    3. Quit

    3. Prompt the user for a single integer that is either 1,2, or 3.
    4. If the user enters an invalid selection, redisplay the menu and prompt them again. Continue this until you get a valid response (1,2,or 3). You must use a loop to do this.
    5.If the user selects 3 then exit the program.
    6.Perform the associated operation (add or subtract) on the previously entered two integers and display the result. Return to step 1 and continue in this manner until the user selects 3 to exit the program.
    7.Name the class CalcJava and save program as CalcJava.java
    8.Submit the program using the WA3 link above.
    So far I have the following code which works fine, seemingly, until I enter some non integer value for the inputs to get added/subtracted or menu choice. In which case errors get thrown out.

    Java Code:
    import java.util.Scanner;
    
    public class CalcJava 
    {
    		
    	public static void main(String[] args) 
    	{
    		
    		int value1 = 0;
    		int value2 = 0;
    		int menuChoice = 0;
    		Scanner input = new Scanner(System.in);
    					
    				
    			
    		while(menuChoice != 3)
    		{
    			
    
    			System.out.printf("Please enter first number : ");
    			value1 = input.nextInt();
    			System.out.printf("Please enter second number : ");
    			value2 = input.nextInt();
    			
    						
    			calcMenu();
    			
    			menuChoice = input.nextInt();
    			
    			if(menuChoice % 1 != menuChoice)
    				calcMenu();
    				
    				
    			switch(menuChoice)
    			{
    			case 1:
    				 calcAdd(value1, value2);
    				break;
    			case 2:
    				 calcSubtract(value1, value2);
    				break;
    			case 3:
    				System.out.println("\tGoodbye!");
    				break;
    			default :
    				System.out.println("\tInvalid input :: Please enter a value of 1, 2, or 3");
    			
    			}
    		};
    		
    			
    		input.close();
    		
    	}
    
    	public static void calcMenu()
    	{
    		System.out.println("\t\t\tWelcome to Java Calculator v1\n\n");
    		System.out.println("\t1. to add two numbers.\n");
    		System.out.println("\t2. to substract two numbers.\n");
    		System.out.println("\t3. to exit.\n");
    		System.out.printf("Please enter a selection: >");
    	}
    	
    	public static int calcAdd(int value1, int value2)
    	{
    		int input1 = value1;
    		int input2 = value2;
    		int resultAdd = 0;
    		resultAdd = input1 + input2;
    		System.out.println("The sum of " + input1 + " and " + input2 + " is > " + resultAdd);
    		return resultAdd;
    	}
    	
    	public static int calcSubtract(int value1, int value2)
    	{
    		int input1 = value1;
    		int input2 = value2;
    		int resultSub = 0;
    		resultSub = input1 - input2;
    		System.out.println("The difference of " + input1 + " and " + input2 + " is > " + resultSub);
    		return resultSub;
    	}
    	
    	
    }
    The error code generated is:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "f"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at CalcJava.main(CalcJava.java:24)
    Now, I'm guess using the Switch case statement is probably a bad idea for this. Also, I don't think getting input values from user before menu selection is a good idea either. I'm thinking while loop is sufficient, but probably should use if statements inside, or some type of nested loops? I'm really having a hard time with this, and the book we use (Java Programming: Joyce Farrell: 9781111529444: Amazon.com: Books)

    Any help or guidance is appreciated.

  2. #2
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with program

    Look into learning how to use Try and Catch effectively to capture this error and respond accordingly. If your code does not have a way to handle errors it will crash.

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Need help with program

    OK. So that exception is saying that Scanner.nextInt() is making a call to Integer.parseInt(String s)... and that is failing because "f" is not an integer.... go figure...

    If you haven't learned about try/catch blocks yet.... well it's never too soon to learn, but your instructor may not be expecting them, so....
    Scanner does have a nifty .hasNextInt() which does this for you!

    Java Code:
                	System.out.printf("Please enter first number : ");
                	while(!input.hasNextInt()){      //While Scanner does not have in int waiting for it, prompt the user to stop messing around.
                		System.out.printf("That's not a number!  Please enter first number : ");
                    	input.nextLine();            //This just grabs the current line... which isn't an int!  (and chucks it)
                    }
                    value1 = input.nextInt();
    etc.
    jace5869 likes this.

  4. #4
    jace5869 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help with program

    Thanks for the help!

    I had sent an e-mail to my professor about the issue as well, and since we haven't covered handling exceptions - it's not required yet. I will take a look at seeing how I can fit the has.NextInt into my code. I really do appreciate the help.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •