Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of bound at index 0?! HELP!

 
sheng liang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Complete code

public class secondTest {

public static ArrayList<String> toInput () {

ArrayList<String> input = new ArrayList<String>();
String temp = "";
Character tempChar = new Character(' ');
Scanner scan = new Scanner(System.in);

temp = scan.nextLine();
tempChar = temp.charAt(0);
while ((temp != null) && (tempChar.compareTo('0') != 0)) {
input.add(temp);
temp = scan.nextLine();
tempChar = temp.charAt(0);
}

return input;

}

public static void main (String[] args) {
ArrayList<String> output = toInput();
//this is a loop to print out the input
for (int i = 0; i < output.size(); i++) {
System.out.println(output.get(i));
}
}


}
 
sheng liang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I posted this thread on both this section and the I/O section, because I am not sure if the problem is on the input part or on the charAt() part.

this is the runtime error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0

it is indicated that the exception is thrown at the place where there is a charAt();
 
sheng liang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's inadequate to post on two forums... I have already announced on the I/O one that that one is not in effect any more.

please answer here.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future reference, you can delete a thread by clicking the little paper-and-pencil icon (), and then setting the "Delete this post" checkbox and clicking the "Save" button.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    sheng liang
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot, Campbell Ritchie, my codes finally work now!!!
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.


    Shouldn't

    "".substring(0)

    execute just fine?
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20512
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes but it doesn't find a substring. "".substring(0) just returns the empty string.

    Well ok, that IS a substring, but you get what Campbell was trying to say.
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you try substring(0) on a String it returns "this". Yes, I suppose substring(0) would work.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic