"ArrayIndexOutOfBoundsException" error in my stack program
I am having these error for quite some time now, and I was getting frustrated as to what causes it. Can you please help me with this? Any kind of help is deeply appreciated. Thanks!
Code:
import javax.swing.JOptionPane;
public class Stack2
{
static String data = "";
static int ctr = 0, maxElem, q;
static String a[] = new String[maxElem];
static int e(String p) //user-defined element value
{
String s = JOptionPane.showInputDialog(p);
int t = Integer.parseInt(s);
return(t);
}
static void Push() //
{
if (ctr == maxElem)
{
JOptionPane.showMessageDialog(null,"The stack is full.");
menu();
}
else
{
data = JOptionPane.showInputDialog(null,"Enter any value(String, integer, etc.):");
a[ctr] = data;
JOptionPane.showMessageDialog(null,"Operation successful.");
ctr++;
Display();
menu();
}
}
static void Pop()
{
if (ctr == 0)
{
JOptionPane.showMessageDialog(null,"The stack is empty. Push some elements first, then retry popping.");
menu();
}
else
{
ctr--;
JOptionPane.showMessageDialog(null,"Popping " + a[ctr] + "...");
a[ctr] = null;
JOptionPane.showMessageDialog(null,"Operation successful.");
Display();
menu();
}
}
static void Peek()
{
if (ctr == 0)
{
JOptionPane.showMessageDialog(null,"There is nothing to display. The stack is empty.");
menu();
}
else
{
ctr--;
JOptionPane.showMessageDialog(null,"The current head of the stack is: " + a[ctr]);
ctr++;
menu();
}
}
static void Display()
{
String d = "";
for (int x = maxElem - 1;x >= 0;x--)
{
d += a[x] + "\n";
}
JOptionPane.showMessageDialog(null, d + "----------------------------\nelement 'null' = empty node");
menu();
}
static void Exit()
{
System.exit(0);
}
static void menu() //menu
{
String m = JOptionPane.showInputDialog(null,"Stack Operations\n[1] Push\n[2] Pop\n[3] Peek\n[4] Display\n[5] Exit\n");
if (m.equals("1"))
Push();
else if (m.equals("2"))
Pop();
else if (m.equals("3"))
Peek();
else if (m.equals("4"))
Display();
else if (m.equals("5"))
Exit();
else
System.exit(0);
}
public static void main(String[] args)
{
q = e("Enter the number of elements for the stack:");
maxElem = q;
menu();
}
}
The error is simple, it means you have stepped beyond or below the size that your array could possibly be, and either tried to read or write data to it. The error should give you an index as well as to which it was and approximate location - this is actually surprisingly helpful in determining where the error can be especially if you use multiple loops within your program.
In this particular instance, its because you have 0 possible entries in your a[]. MaxElem has not been defined as any particular value by this point, so by default its size is 0. Not sure how large you want that to be though...
Using arrays as stacks or queues is a bit of a pain since you need to resize them, move them, copy them, and do all sorts of things to them. I would suggest changing the String array into a collection such as an ArrayList<String> or a Vector<String> (the overhead is a trade off for simplicity). These lets you move things in and out without needing to resize them manually. For some extra experience, you may want to look at writing your own linked lists. They are about the easiest datastructure to write, and carry a lot of useful features (using a double linked list lets you move from the front and end of a linked list with a O(1) magnitude, removing / adding from middle of collection takes minimal effort). If you do that, consider extending the AbstractCollection class (or implementing the Iterator and Collection interfaces) to create an iterable object that can be used in things like a for each. Very handy.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php