Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation "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();
    	}    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •