Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Exception Handling with command line error

ScottBondScottBond Posts: 1Member
Hello I am making a program which is basically a calculator using the command line. It works fine for the most part but part of the requirements for the program is to use exception handling to check if one of the characters entered is a letter, and if it is a letter to give them an error, and this is where I am getting my error. It does not seem to like the, Character.isLetter(args[i])), I think it is because the Character.isLetter() method takes a single char value as an argument.. But in this case I am not sure how to evaluate a single char value, I would really appreciate any help!

My error is:
CalculatorWithException.java:16: cannot find symbol
symbol : method isLetter(java.lang.String)
location: class java.lang.Character
if(Character.isLetter(args[i]))
^
1 error

my code so far is:

[code]
public class CalculatorWithException
{
public static void main(String[] args)
{
if(args.length != 3)
{
System.out.println("Usage: Java Calculator operand1 operator operand2");
System.exit(0);
}
try
{
for(int i=0; i < args.length; i++)
{
if(Character.isLetter(args[i]))
{
throw new ArithmeticException("Must enter a number");
}

}
}

catch(ArithmeticException ex)
{
System.out.println("You entered a non numeric character");
}
int result = 0;

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);
}
}[/code]
Sign In or Register to comment.