aspose file tools*
The moose likes Beginning Java and the fly likes String index out of bound at index 0?! HELP! 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 bound at index 0?! HELP!" Watch "String index out of bound at index 0?! HELP!" New topic
Author

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

sheng liang
Greenhorn

Joined: May 03, 2007
Posts: 11
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

Joined: May 03, 2007
Posts: 11
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

Joined: May 03, 2007
Posts: 11
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
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

    Joined: May 03, 2007
    Posts: 11
    Thanks a lot, Campbell Ritchie, my codes finally work now!!!
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    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 ]

    "I'm not back." - Bill Harding, Twister
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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?


    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
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19790
        
      20

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    If you try substring(0) on a String it returns "this". Yes, I suppose substring(0) would work.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: String index out of bound at index 0?! HELP!