Hi, this problem has been bugging me for days now.
Basically I try to read a file and store the data into an array of strings or ArrayList<String>, and then get the first character of each element of String. There occurs a runtime exception that StringIndexOutOfBound at 0?!
Can someone please take a look at my code and probably save me out of this
Please don't post it twice. I suggest you delete the post from the I/O forum.
StringIndexOutOfBoundsException at position 0 means you are trying to get the first (position 0) character out of a String which doesn't have a first character.
"How can that be?" they ask, "doesn't every String have a first character?"
No, this String hasn't : "". There is such a thing as an empty String or a zero-length String which is represented by the String literal "". It hasn't got any characters in, but still exists as a String object. You can do things like myString.length() on it, which should return 0. But you can't find any characters or substrings or anything in it. So if you try to find charAt(0) it will throw the exception you are getting.
Where are you getting the exception? Is it where you are reading the line from the Scanner? You might be entering a "new line" character without entering any content for the line. That would give a zero-length String as a result.
BTW: You will find it a lot easier to use !tempChar.equals('0') than tempChar.compareTo('0') != 0. You can make it even simpler by using boxing and un-boxing (not available in JDK1.4) and use tempChar == '0'. And you can probably declare tempChar as a char rather than a Character.
Joined: Oct 13, 2005
Try this sort of heading for your while-loop
A little bit of awkward syntax, maybe the one bit of awkward Java syntax worth learning by heart.
It does the following:
Gets the next line. This is assigned to the variable temp, inside round brackets () because the = operator has the lowest priority of anything.
Checks that this line is not null. If it is, it terminates the "while".
Checks that the line is not zero-length; if it is, terminates the "while".
Gets the first character of the line and assigns it to the tempChar variable. Again in round brackets () because of the low priority of the = operator.
Checks with tempChar that the line does not begin with the character 0. If it does, terminates the "while".
If you do the checks in that order, you won't have any problems with null exceptions or zero length exceptions.
You don't appear to use the tempChar variable elsewhere. You can dispense with it altogether by changing the tempChar test to read
Joined: May 03, 2007
Thanks a lot, Campbell Ritchie, my codes finally work now!!!
The original error message would have contained information about the line number that was throwing the exception. It would have pointed you to this line:
The basic problem here is that you are assuming that there is at least one character in the string, at index 0. The error message is telling you that there isn't one - index 0 is out of bounds because there's no character there. Campbell's code shows one way of avoiding this. But I think it's worthwhile to understand that the original error message was telling you everything you needed to know. [ December 24, 2007: Message edited by: Jim Yingst ]
Originally posted by Campbell Ritchie: There is such a thing as an empty String or a zero-length String which is represented by the String literal "". It hasn't got any characters in, but still exists as a String object. You can do things like myString.length() on it, which should return 0. But you can't find any characters or substrings or anything in it.
execute just fine?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus