File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array lengths Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array lengths" Watch "Array lengths" New topic
Author

Array lengths

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
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...


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
array has a fixed length

try ArrayList instead


java amateur
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
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

Joined: Feb 08, 2004
Posts: 1281
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array lengths