This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Array lengths

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
i have an array of values which refrence String objects, and i need to increment the unique value if the element in the array is not null which it all works fine when i set the array size to say three, but if i increase it to say 10 it crashses. any ideas on my problem...
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
array has a fixed length

try ArrayList instead
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see what the ArrayList suggestion has to do with the original problem.

but if i increase it to say 10 it crashses.



How about some details about what "crashes" means? Post some code. Post your error. No one here is a mind reader.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the lack of info
any way here is the error if i set the array size 10.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at arrays.Storage.addStudent(Student.java:69)
at arrays.StudentTest.main(StudentTest.java:19)
this happens after i have added five new names to the array

here is the add method i have:

public void addStudent(Student studentObj, int count)
{

int hash = studentObj.hashCode(numOfStudents);
int index = 0;
boolean add = false;
if (students[hash] != null)
{
while (students[hash] != null && hash < students.length)
{
++hash;
System.out.println("++i");
}
students[hash] = studentObj;
}
else
{

students[hash] = studentObj;
}

and the test:
class StudentTest
{

public static void main(String[] args)
{
int maxNum = 10;
Storage store = new Storage(maxNum);

for (int i = 0; i < store.getSize(); i++)
{
Student name = new Student(JOptionPane.showInputDialog(
"please enter a name!").toUpperCase(), JOptionPane
.showInputDialog("please enter a course!")
.toUpperCase());
store.addStudent(name, maxNum);
}

System.out.println("Student name\tStudent course");
System.out.println("------------\t--------------");
for (int i = 0; i < store.getSize(); i++)
{
System.out.println( store.retrieveList(i).getName() + "\t\t"
+ store.retrieveList(i).getCourse());
}
System.exit(0);

}

}

thanks again
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't see what the ArrayList suggestion has to do with the original problem.


i understood OP was trying to add new items to an array
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic