I'm trying to run through a file and add each string in the file to a string array.

My code yields the error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at StudentLAM.main(StudentLAM.java:14)"

Why doesn't this work? Note: I cannot use ArrayList.
My error-yielding code:

private Scanner index;
int cell = 0;
public String[] rawData = new String[count]; //the int count has been set to equal the number of strings in the file; I didn't include its instantiation method

	while (index.hasNext()) {
		rawData[cell] = index.next();
		cell++;
	}

	for (int x = 0; x <= count; x++) {
		System.out.println(rawData[x]);
	}

I omitted a lot of code for brevity's sake.

Recommended Answers

All 2 Replies

I'm trying to run through a file and add each string in the file to a string array.

My code yields the error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at StudentLAM.main(StudentLAM.java:14)"

Why doesn't this work? Note: I cannot use ArrayList.
My error-yielding code:

private Scanner index;
int cell = 0;
public String[] rawData = new String[count]; //the int count has been set to equal the number of strings in the file; I didn't include its instantiation method

	while (index.hasNext()) {
		rawData[cell] = index.next();
		cell++;
	}

	for (int x = 0; x <= count; x++) {
		System.out.println(rawData[x]);
	}

I omitted a lot of code for brevity's sake.

I tried rewriting your code. You only had minor logical errors on your code. this should work perfectly, I know cuz I tried running it on my textpad. See comments.

private Scanner index;
int cell = 0;
public String[] rawData = new String[count]; 

	while (index.hasNext()) {
              // I put an if statement to stop the loop from going over the actual  array count, so then it wont throw an indexoutofbounce exception.
                 if(cell < count){
		rawData[cell] = index.next();
		cell++;
	         }
	    else
	    break;    
	}

	for (int x = 0; x < count; x++) { // took out the = sign, arrays starts at 0.
		System.out.println(rawData[x]);
	}

Hope You'll give me a solved thread points. Thanks in advance.

commented: Nice catch on the if statement +0
commented: Please don't do people's homework for them. Give help, not code. +0

How do you get an indexoutofbounds exception for an index of zero? I suspect that count was inadvertantly initialized to zero, which means there isn't any array there.

The main problem visible in the original posting - which jkp corrected, but failed to call attention to, is the for loop running from 0..count (x<=count) rather than 0..count-1 (x<count). This is guaranteed to run over the array bounds, because array[array.length] is by definition not an element of the array.

If you know the number of elements in advance, declaring an array of that length is fine, no need to check anything. If you don't, and you want to use an array, it's easy enough to double an array's length. Just make a new array of double the length, copy the values from the old array to the new, and return the new array, assigning it to the old one. Check the length and call the double method if it's going to take you out of bounds. Checking and exiting without warning is pretty lousy design - you've failed to do what you set out to do, and the user has no way of knowing that only part of their data has been read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.