• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't solve error in code

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just note, "Can't solve error" is a really vague title on a programming forum...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic