This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't solve error in code

 
stefan balling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you set a default capacity of 10? And where are you enlarging your array when you pass the 10th element?
 
stefan balling
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7327
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 2942
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just note, "Can't solve error" is a really vague title on a programming forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic