We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

Eindoofus
Eindoofus asked
on
Medium Priority
328 Views
Last Modified: 2012-08-13
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?
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:

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

Maybe that 's what he meant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.