Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2011, 10:23 AM   PM User | #1
weej25aya
New to the CF scene

 
Join Date: Feb 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
weej25aya is an unknown quantity at this point
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();
	}    
}
weej25aya is offline   Reply With Quote
Old 02-08-2011, 02:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
error, java, program, stack

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 AM.


Advertisement
Log in to turn off these ads.