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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cainsawAuthor Commented:


It works.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.