Why is this code failing to compile and how can I fix it?

When I compile the code below on IDEONE.COM I get the following errors:
Main.java:37: cannot find symbol
symbol  : class StackException
location: class hw2
      catch (StackException se) {
             ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Open in new window

This is the code I'm trying to compile:
import java.util.Stack;
 
class hw2 {
 
   public static void main (String[] args) {
      
      Stack < Integer>s = new  Stack();
                
      try {
         for (int i = 0; i < args.length; i++) {
            if (args[i].equals("+")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(a + b));
            }
            else if (args[i].equals("-")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b - a));
            }
            else if (args[i].equals("*")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b * a));
            }
            else if (args[i].equals("/")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b / a));
            }
            else {
               s.push(new Integer(args[i]));
            }
         }              
         System.out.println(((Integer) s.pop()).toString());
      }
      catch (StackException se) {
         se.printStackTrace();
      }
   }
}

Open in new window


This code was given to us by our instructor for an infix-postfix homework assignment. There was never any "StackException" classes given in this course and I doubt that he intended to make that part intentionally more complicated since it's simply a Data Structures and Algorithms course.

Why isn't this code compiling? Am I failing to import something? If not, then what is a proper substitute for "StackException"? Why is that try/catch even there?
EindoofusAsked:
Who is Participating?
 
Justin MathewsConnect With a Mentor Commented:
Did you mean EmptyStackException? See below:


import java.util.Stack;
import java.util.EmptyStackException;
 
class hw2 {
 
   public static void main (String[] args) {
      
      Stack < Integer>s = new  Stack();
                
      try {
         for (int i = 0; i < args.length; i++) {
            if (args[i].equals("+")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(a + b));
            }
            else if (args[i].equals("-")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b - a));
            }
            else if (args[i].equals("*")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b * a));
            }
            else if (args[i].equals("/")) {
               int a = ((Integer) s.pop()).intValue();
               int b = ((Integer) s.pop()).intValue();
               s.push(new Integer(b / a));
            }
            else {
               s.push(new Integer(args[i]));
            }
         }              
         System.out.println(((Integer) s.pop()).toString());
      }
      catch (EmptyStackException se) {
         se.printStackTrace();
      }
   }
}

Open in new window

0
 
for_yanCommented:

there exists EmptyStackException
http://download.oracle.com/javase/1.4.2/docs/api/java/util/EmptyStackException.html

Maybe that 's what he meant
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.