[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Loading a Stack (keep getting java.util.EmptyStackException)

Posted on 2004-11-28
Medium Priority
Last Modified: 2008-03-04
I had this difficulty on another program and I was unable to solve it.  I am getting an EmptyStackException.  I know this is because my stack is not “filling up”.   It compiles but it does not run.

C:\forte4j\Assignment 3>javac Scan.java

C:\forte4j\Assignment 3>java Scan
Exception in thread "main" java.util.EmptyStackException
        at java.util.Stack.peek(Stack.java:79)
        at java.util.Stack.pop(Stack.java:61)
        at Scan.main(Scan.java:42)

Basically, I am trying to compare a number.  If the number is not the number I want, then I put it on a stack and then “pop” the whole stack and check the numbers and those numbers that do not match get put onto a Temp stack.  Then, I want to “pop” the Temp stack and put all these numbers back onto the original stack so I can begin the process again.  (Once I get this to work, I still have a couple more loops to go.  However, as I have stated previously, I don’t want to get too far ahead of myself and have a zillion errors to fix.)

The code:

import java.io.*;

import java.util.*;

public class Scan {

      public Scan () {}

      public static void main (String args [])

          Scan Roll = new Scan ();
          Scan T_Unit = new Scan ();

          Stack ScanStorage = new Stack ();
          Stack Temp = new Stack ();

          int Track = 1;
          double prevNum = 0;
          double AvgWait = 0;
          double AccumTime = 0;
          int Serviced = 0;
          int count = 0;

          Integer Store = new Integer (0);
          Integer Tracksome = new Integer(0);

          while (count < 100)
            if (count < 40)
            {//beginning count <40 "if"

                if (Track == Roll.ScanRoll ())
                  Serviced ++;
                if (Track != Roll.ScanRoll ())
                  ScanStorage.push (new Integer (Roll.ScanRoll ()));

                  while (! ScanStorage.empty())
                  { //beginning ScanStorage !empty "while"

                      Store = (Integer)ScanStorage.pop();
                      ScanStorage.pop ();

                        Track = Tracksome.intValue();
                        if (Tracksome == Store)
                            Serviced ++;
                                 Temp.push (Store);
                  } //end ScanStorage !empty "while"

                  while (! Temp.empty())
                  { //beginning Temp !empty "while"

                      Store = (Integer)Temp.pop();
                        ScanStorage.push (Store);
                  } //end Temp !empty "while"

                Track ++;

            }// end Track <40 "if"

            prevNum = T_Unit.TimeUnit();
            AccumTime = AccumTime + prevNum;

            count ++;

          }//end count "while" loop

            //AvgWait = AccumTime/count;
            //System.out.println("Avgwait is " +AvgWait);

      }//end of main

      public static int ScanRoll()
          int TheRoll;
          TheRoll = 1 + (int)(Math.random() * 40);

          return TheRoll;

      }//end ScanRoll

      public static int TimeUnit ()
          int Time;
          Time = 1 + (int)(Math.random() * 10);

          return Time;

      }//end TimeUnit

}//end of Scan
Question by:cainsaw

Accepted Solution

udo_borkowski earned 600 total points
ID: 12692919
The reason why you get the EmptyStackException is because you pop the stack twice instead of once to get an element.


                   Store = (Integer)ScanStorage.pop();
                   ScanStorage.pop ();


                   Store = (Integer)Temp.pop();

The correct code would be:

                   Store = (Integer)ScanStorage.pop();


                   Store = (Integer)Temp.pop();

Author Comment

ID: 12698178


It works.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question