File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can't solve error in code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't solve error in code

stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
Hi

I'm currently working on a uni exercise which I have finished but I keep getting an error when I compile an array greater than 10. I am creating an ArrayQueue. It should act as a stack basically and I was asked to implement these methods and test on data samples he gave me. Apologizes if I upload this wrong. My code is:




My error when I run an array of size 10 or above is an ArrayOutOfBounds exception. I know why that exception is cropping up in theory but I don't see where my code is causing this.

Thanks for any help.

Stefan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
So you set a default capacity of 10? And where are you enlarging your array when you pass the 10th element?
stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
I'm not, the class variables were declared before we started. We only implemented the methods. I had a feeling that was the issue but the test program was throwing up data larger than 10 so do you think there could be an error in the testing data sizes?

Thanks
stefan balling
Greenhorn

Joined: Nov 28, 2012
Posts: 7
I've actually made a few changes to improve the code. I actually code the enqueue and dequeue the wrong way round. The rear was the last element in the queue therefore if would be the first to be removed. The problem now is every time I add a new element in, I get the element plus the next element added as null. I.e I add cat and I get (cat, null) displayed.. Anyone could help with that?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Arrays in Java are static in size: once you declare them to have a certain number of elements, that's it. They don't automatically grow. If you use the no-argument constructor, then your array will only have 10 elements. If you use the constructor that takes an initial capacity, the array will only have that many elements. As Campbell alluded to before, there is nothing in your code that increases the size of your array once it has been filled up. Incrementing the 'size' variable doesn't do it.


Junilu - [How to Ask Questions] [How to Answer Questions]
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
If you fill the array from the 0 index you don't need to use rear, front and size variables, you just need a single variable to denote the next available location in the stack. The variable starts at 0 and provides the index to insert the next enqueued element and is then incremented. It's value also gives you the current stack size. And to dequeue an element you decrement the variable and remove and return the element at that index. Obviously, you also need to do bounds checking before adding or removing elements.

BTW in future when posting, if you are getting an exception please include the full exception message and stack trace.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3486
    
  13
stefan balling wrote:The problem now is every time I add a new element in, I get the element plus the next element added as null. I.e I add cat and I get (cat, null) displayed..

The null isn't being added - the array elements will be null by default. The actual problem is your toString method that is printing out size + 1 elements.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
stefan balling wrote: . . . an ArrayQueue. It should act as a stack basically . . .
In which case, why are you calling it ArrayQueue? Stacks are Stacks and Queues are Queues and the two behave differently.
There are all sorts of other style problems, for example using an if-else to return a boolean. Look here for what you ought to write.
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

Just note, "Can't solve error" is a really vague title on a programming forum...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't solve error in code