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

Posted on 2004-11-28
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

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

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.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
    LVL 3

    Accepted Solution

    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



    It works.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    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…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now