Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a problem with this code:

Java
public class Calculator
{
    public static void main(String[] args)
    {
        try
        {
            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]);
                            break;
                }
                System.out.println(args[0] + ' ' + args[1] + ' ' + args[2] + " = " + result);
            }
            catch(NumberFormatException ex)
            {
                System.out.println(ex.getMessage());
                System.exit(0);
            }
        }
        catch(Exception ex)
        {
            System.out.println("You have to enter it in as: Number1 operator Number2");
            System.exit(0);
        }
  }
}


The problem is that when I toss in 3 + 3 in the command line it works but if I write 3 * 3 the arg array prints out the Calculator.class, why does this happen?

Also Is there a way in NumberFormatException catch to print out the offending input?
Posted
Comments
Richard MacCutchan 5-Sep-10 13:04pm    
As to your second question you never get the exception because you code falls through if the first character of args[1] is not one of the valid operators.
[no name] 5-Sep-10 13:09pm    
But if args[1] was a valid operator, how could I do it then.
Or for exceptions in general how does one get the offending input?

The problem is that the * character is interpreted by the shell and is replaced by a list of the files and directories in the directory that the call is made from. You need to use another character to represent multiply or submit the * within " (double quote) characters thus:
java Calculator 3 "*" 3
// or using x character
java Calculator 3 x 3
 
Share this answer
 
v2
Comments
[no name] 5-Sep-10 12:51pm    
Thanks a lot, I figured out I could do "*" but I couldn't understand why it worked.
So thanks for the answer.
Dalek Dave 5-Sep-10 13:42pm    
Good Answer
How about firing up a debugger, setting some breakpoints, and the like?

Just use the free Visual Studio Express Edition, if you have no such tool yet.

Cheers
Uwe
 
Share this answer
 
Comments
[no name] 4-Sep-10 15:17pm    
I don't have any debuggers and wasn't java discontinued from Visual Studio Express, I could be wrong though and would love a link to it if you have the time.
Also I know the problem, I just don't know what causes it. The calculator works for /,+,- but for some reason it doesn't take in *. The * operator does work however if I enclose it with " ": like 3 "*" 3. Could it be that regular expression is messing with this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900