aspose file tools*
The moose likes Beginning Java and the fly likes String Index Out Of Bounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Index Out Of Bounds Exception" Watch "String Index Out Of Bounds Exception" New topic
Author

String Index Out Of Bounds Exception

Elmer Haytom
Greenhorn

Joined: Mar 02, 2010
Posts: 4
I'm really confused. I'm trying to read a matrices from a text file, and I keep getting errors. The error always comes out in the last line of that for loop with the charAt(z) part.
The error says: java.lang.StringIndexOutOfBoundsException: String index out of range: 0. I want to read each individual character in each row.

Notes: I've already read the column and rows variables from the text file, and read in the variable input from the user earlier in the code and that part works fine. It's this for loop I'm having problems with. This is obviously not the full code, just the troublesome section.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try to output garbage before assigning the char to the matrix.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 413

I am not sure probably this is happening in your case.



You must be getting an empty string in garbage. That is the reason of Exception. So if string is empty it will not find any value at index 0. Like in above program for first value in names array you will get



if you remove that first element from array you will get



But for the last element you will not get any exception becuase the number of columns are equal to the size of string.

You can remove this exception using following code;



>


Pawan Chopra
SCJP - DuMmIeS mInD
Elmer Haytom
Greenhorn

Joined: Mar 02, 2010
Posts: 4
hmmm, yes, there is a line between the first stuff i'm reading from the file and the matrix. I tried:

garbage = textfile.nextLine();

before the for loop (with the for loop commented out), then printing garbage and it printed out a bunch of lines with nothing. Then I tried:

garbage = textfile.nextLine();
garbage = textfile.nextLine();

before the for loop. My idea was that the first line would get rid of the empty line in the text file, and then the second line would print it out, but it still printed a bunch of empty lines.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Elmer Haytom
Greenhorn

Joined: Mar 02, 2010
Posts: 4
Well the problem is, I think, that the text file is like this:


I need to get past that empty line with my scanner in order to read the matrix. Using .isEmpty in the way you wrote seems like it would make my program not error when compiling, but I it won't go into the for loop unless I get past that emply line, and calling .nextLine() an extra time doesn't seem to be getting past that empty line like I thought it would.


Elmer Haytom
Greenhorn

Joined: Mar 02, 2010
Posts: 4
NM, guys. Got it working. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to the Ranch

Please avoid abbreviations like "NM".

How did you sort out your problem? Please show us. I would have put in a test like while ((input = myScanner.nextLine()).length() == 0);
Note the ; at the end of the "while", a feature you must mark with a comment like /* empty */; this means do nothing as long as you have a zero-length string read from the file. The length() and while and == won't cause a performance overhead. Once you have a non-zero-length string, it is assigned to "input" ready for use.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: String Index Out Of Bounds Exception