Results 1 to 3 of 3
  1. #1
    EZ-Ed is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Need help on my excpetion handing assignment

    The purpose of the assignment is to make a calculator which terminates if any operand is non-numeric. Also the program must use exception handlers in order to deal with non-numeric operands. I have a "working" program, however my teacher told me that where my try statement is I should use an if statement and move the try statement below my cases. My teacher speaks in broken English so it's hard for me to fully understand his explanations. Here is my code, any insights?
    Java Code:
       import javax.swing.*;
       public class Calculator {
       
          public static void main(String[] args) {
          
             if (args.length != 3) {
                System.out.println(
                   "Usage: java Calculator operand1 operator operand2");
                System.exit(0);
             }
          
          
             int result = 0;
          
             try{     
    			  	 switch (args[1].charAt(0)) {
                   case '+': result = Integer.parseInt(args[0]) +
                             Integer.parseInt(args[2]);
                      break;
                   case '-': result = Integer.parseInt(args[0]) -
                             Integer.parseInt(args[2]);
                      break;
                   case '*': result = Integer.parseInt(args[0]) *
                             Integer.parseInt(args[2]);
                      break;
                   case '/': result = Integer.parseInt(args[0]) /
                             Integer.parseInt(args[2]);
    					
    					default: System.out.println("Error, bad operation code");
    					System.exit(0);									 
                
             	}
             
                System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
                   + " = " + result);
             
        			}
    			      
    				
    				catch(NumberFormatException numfex){
                   JOptionPane.showMessageDialog(null, "Error, you can not do simple math with letters");}
          
    				catch(ArithmeticException ex){
    					JOptionPane.showMessageDialog(null, "Error, you can not divide by zero");}
    								
    			
    		}
    		   
       }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Need help on my excpetion handing assignment

    I'm not sure.
    They seem to be (from your description) implying you should have the try/catch inside each case statement, but that strikes me as overkill.
    Or, you could parse the two integers before entering the switch, thus only having the one parsing statement?

    I'm really guessing here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Need help on my excpetion handing assignment

    If I were your teacher I would first ask you to follow coding conventions in respect of indentation and whitespace, and only look at the code after you complied.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. spring mvc form handing
    By BALUBALU in forum Spring
    Replies: 3
    Last Post: 08-08-2012, 06:01 PM
  2. Error Handing, identifying offending string
    By whytefish in forum New To Java
    Replies: 9
    Last Post: 03-16-2011, 12:28 PM
  3. Help with an assignment
    By aqeel2010 in forum New To Java
    Replies: 5
    Last Post: 01-14-2011, 09:04 AM
  4. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM
  5. Need help with a assignment
    By helpMe.Java in forum New To Java
    Replies: 7
    Last Post: 06-06-2010, 04:49 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
  •