I'm trying to make a program that reads in numbers and commands and prints the list of numbers.
I am trying to solve one problem at a time and this is about exception handling. I am still not familiar with the whole exception handling that is why I get confused.
example:
reads in 1 2 3 + p abc
the + sign means adding 3 and 2
the p means printing the numbers
and abc is the one I want to catch
I did an IMM exception, however, I just want to know if there is another way to doing this. Or is creating my own exception going to be better?
public static void main(String[] args) {
int num;
Scanner input = new Scanner(System.in);
MyStack stack = new MyStack();
while(input.hasNext())
{
try
{
if(input.hasNextInt()){
num = input.nextInt();
stack.push(num);
}
else if (input.next() == "+"){
//stack.add();
}
}
catch (InputMismatchException ex)
{
System.out.println("too long: ");
break;
}
}