• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 67671
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
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic