• 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

Sorting a numerical Array

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
I am working on a small project for school with the following requirements:
- Create a program that requests the user to enter five integers
- Place these values into an array called myArr
- Type any sorting method into this program that will sort the array myArr
Here is my code:

This is the exception I am getting:


I am lost =(
Any help or suggestions would be much appreciated!
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Halo,

The java.lang.ArrayIndexOutOfBoundsException means that your are trying to access an array object with an index larger than the current data inside the array.

Array in Java is zero indexed, which means the array index start from zero. So if you have five items in it (array length = 5) it means the index if from 0 to 4. In your code you have five items in the array, this mean the maximum array index is 4.

To fix the error in your code you have to change the for loop condition check in insertionSort() method.

Regards,
Wayan
 
Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic