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 out of bounds error for unknown reason Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array out of bounds error for unknown reason" Watch "Array out of bounds error for unknown reason" New topic
Author

Array out of bounds error for unknown reason

Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

I'm getting an ArrayOutOfBoundsException. My code:






I have left out the try/catch blocks in order to get the exception, but I don't understand why the error.
After the split, the parameters are for the delimiter and the size of the array, right? Is this not where I
declare the size of the array? I'm trying to input from a text file with the name of a photo, the photographer's
name, the file type, and a double for the file size. It is delimited by "|" and I want to create an array to hold
this information of four elements.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Marjorie Gyles wrote:I'm getting an ArrayOutOfBoundsException. My code:






I have left out the try/catch blocks in order to get the exception, but I don't understand why the error.
After the split, the parameters are for the delimiter and the size of the array, right? Is this not where I
declare the size of the array? I'm trying to input from a text file with the name of a photo, the photographer's
name, the file type, and a double for the file size. It is delimited by "|" and I want to create an array to hold
this information of four elements.


No... the second parameter is a limit on the split -- meaning after the split is done, the array will be at most 4 elements. If there aren't three delimiters, then it is possible to have less than four elements.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I would always suggest you use System.out.println() to see what your code is doing. You can always take them out.

I would suggest you print the size of the array and see if it really is four elements each time. Due to the NPE, it must not be.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jinu Vijay
Greenhorn

Joined: Sep 19, 2011
Posts: 17

Please check the validity of the line being read from the file by an SOP. A sample code is below for reference.

String str = "photoName|photographerName|fileType|20.2";
String[] fields = str.split("\\|", 4);
System.out.println(fields[0] + " " + fields[1] + " " + fields[2] + " "
+ Double.parseDouble(fields[3]) + " ");

Output:
photoName photographerName fileType 20.2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Jinu Vijay welcome to the Ranch
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

I'm getting the error message all over my input when I add the System.out.print statement.
I'm not supposed to put a delimit symbol at the end of the lines am I? I thought that was what
the enter character was for. I tried the suggestion from Jinu and it worked perfectly, so at this
point I'm wondering what's wrong with my input file.


Error output:
My Kittens Joe Miller jpeg 1.4
Beauty Spot Marty Barrett jpeg 2.1
Funny Bob Farnsworth gif 0.5
Albino Possum Don Bailes png 3.9
Xylophone Alvin Chipmunk gif 0.8
Colorful Zombie Elliemae Clampett png 2.2
Barracuda Ben Dover jpeg 8.7
Fig Newton Willie Makit png 4.3
Sunset Betty Wont jpeg 6.1
4-eye Butterfly fish Claude Badly jpeg 9.2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PhotoTracker.main(PhotoTracker.java:94)



Line causing the problem:




Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Could you share the input file contents you are trying to tokenize? I would like to see that.


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
You are trying to get the 4th element in a 3‑element array. Before that statement, print out the line you are splitting, and the whole array after splitting, using the Arrays#toString() method, and its length field.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Mansukhdeep Thind wrote:Could you share the input file contents you are trying to tokenize? I would like to see that.


The "error output" listing is the information I am trying to import.
"My Kittens Joe Miller jpeg 1.4" etc...
I don't understand why it's importing it, printing it, and still giving me an error message.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3950
    
  17

Marjorie Gyles wrote: I don't understand why it's importing it, printing it, and still giving me an error message.

My guess is there is an error on the input - instead of getting 3 delimeters, you only get 2. You need to do what Campbell asked you to do.


Steve
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Campbell Ritchie wrote:You are trying to get the 4th element in a 3‑element array. Before that statement, print out the line you are splitting, and the whole array after splitting, using the Arrays#toString() method, and its length field.




Gets me:
My Kittens
Joe Miller
jpeg
1.4
Beauty Spot
Marty Barrett
jpeg
2.
Funny
Bob Farnsworth
gif
0.5
Albino Possum
Don Bailes
png
3.9
Xylophone
Alvin Chipmunk
gif
0.8
Colorful Zombie
Elliemae Clampett
png
2.2
Barracuda
Ben Dover
jpeg
8.7
Fig Newton
Willie Makit
png
4.3
Sunset
Betty Wont
jpeg
6.1
4-eye Butterfly fish
Claude Badly
jpeg
9.2
My Sheep
Bo Peep
12
>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Notice there is a break in the pattern

My Sheep
Bo Peep
12
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

I would not have thought that would give an array out of bounds error. I would think more along the lines of a bad number format instead. I should be able to catch it and still write the rest of the text into the array, though, right? (I'm currently sitting in a classroom and am typing on my nook, so I can't play with the code at the present time.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Marjorie Gyles wrote:I would not have thought that would give an array out of bounds error.

Marjorie,

You still haven't supplied us with the input that caused the error. Until you do, we're just guessing.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Winston Gutkowski wrote:
Marjorie Gyles wrote:I would not have thought that would give an array out of bounds error.

Marjorie,

You still haven't supplied us with the input that caused the error. Until you do, we're just guessing.

Winston


Sorry, Winston, I thought I made it clear. The Input file...

My Kittens|Joe Miller|jpeg|1.4
Beauty Spot|Marty Barrett|jpeg|2.
Funny|Bob Farnsworth|gif|0.5
Albino Possum|Don Bailes|png|3.9
Xylophone|Alvin Chipmunk|gif|0.8
Colorful Zombie|Elliemae Clampett|png|2.2
Barracuda|Ben Dover|jpeg|8.7
Fig Newton|Willie Makit|png|4.3
Sunset|Betty Wont|jpeg|6.1
4-eye Butterfly fish|Claude Badly|jpeg|9.2
My Sheep|Bo Peep|12
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Notice on the last line, you only have 3 tokens and not 4. So the actual array is only going to contain 3 members not 4, so 3 is not a valid index.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Keith Lynn wrote:Notice on the last line, you only have 3 tokens and not 4. So the actual array is only going to contain 3 members not 4, so 3 is not a valid index.


After you pointed that out, I realized you were right. However, I don't know how to get around the bad input other than to catch it.
If I try and catch it, I won't be able to input the rest of the file. How do I ignore this particular input and input the rest of the file?
He never taught us that and I'm having a hard time finding the answer anywhere.
Jinu Vijay
Greenhorn

Joined: Sep 19, 2011
Posts: 17

Marjorie Gyles wrote:I'm getting the error message all over my input when I add the System.out.print statement.
I'm not supposed to put a delimit symbol at the end of the lines am I? I thought that was what
the enter character was for. I tried the suggestion from Jinu and it worked perfectly, so at this
point I'm wondering what's wrong with my input file.


Error output:
My Kittens Joe Miller jpeg 1.4
Beauty Spot Marty Barrett jpeg 2.1
Funny Bob Farnsworth gif 0.5
Albino Possum Don Bailes png 3.9
Xylophone Alvin Chipmunk gif 0.8
Colorful Zombie Elliemae Clampett png 2.2
Barracuda Ben Dover jpeg 8.7
Fig Newton Willie Makit png 4.3
Sunset Betty Wont jpeg 6.1
4-eye Butterfly fish Claude Badly jpeg 9.2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PhotoTracker.main(PhotoTracker.java:94)



Line causing the problem:







Here ... after the below ...



you could first check the length of fields (i.e. fields.length) and if it is < 4 may be log the error, and continue the loop to read the next line.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Jinu Vijay wrote:you could first check the length of fields (i.e. fields.length) and if it is < 4 may be log the error, and continue the loop to read the next line.


Thanks! That worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array out of bounds error for unknown reason
 
Similar Threads
Confused with file read
NullPointerException
Why am I getting a Null Pointer Exception???
Array Index Out Of Bounds Exception
request.getParameter() returning null