I'm really stuck. I only want this program accept integer input. Any other input like a char I want it to display a message that it is the wrong type of input and display the input in question. Here is what I've got so far. Any help would be appreciated.
Thanks,
Marie
//ExceptDemo1b.java
public class ExceptDemo1b
{
//Main Method with three arguments
//args[0]: operator
//args[1]: operand1
//args[2]: operand2
public static void main(
String[] args)
{
//Declare and initialize variables
int result = 0;
int operand1 = 0;
int operand2 = 0;
if (args.length != 3)
{
System.out.println("Usage:
java Calculator operator operand1 operand2");
System.exit(0);
}
if(((operand1 >= 0) || (operand1 < 0)) && ((operand2 >= 0) || (operand2 < 0)))
{
operand1 = Integer.parseInt(args[1]);
operand2 = Integer.parseInt(args[2]);
switch (args[0].charAt(0))
{
case '+': result = Integer.parseInt(args[1]) +
Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[1]) -
Integer.parseInt(args[2]);
break;
case '*': result = Integer.parseInt(args[1]) *
Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[1]) /
Integer.parseInt(args[2]);
}
//Display the result
System.out.println(args[1]+ ' ' +args[0]+ ' ' +args[2]+ "=" +result);
}
else
{
System.out.println("Wrong input ");
System.exit(0);
}
}
}