• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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?
0
Eindoofus
Asked:
Eindoofus
1 Solution
 
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
 
Justin MathewsCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now