String
Hello Java Friends,
I thought I will get over this problem as soon as possible but I was totally wrong.
My plans was to split a text file in this format;
1 ?a, d, f, h,
2 ?Obama, gate, money
3 ?java, string, split, all
and store the first Strings ( 1, 2, 3 ) in a Vector container and rest tokens in another Vector container. I was able to split and store the first strings but my problem is that it always show "java.lang.ArrayIndexOutOfBoundsException: 1
Please see the code below:
line = br.readLine();
while (line !=null){
lineData = line.split("\n ");//split the lines"
String []wordings =null;
String []bex =null;
for(int i = 0; i < lineData.length ; i++){
bex =lineData[i].split("?);//split at "?
}
NumberOfLines.add(bex[0]);//store the first line values in a String Vector
wordings = bex[1].split(",");// Here is the problem !!
for (int k = 0; k < allWords.size(); k++)
{
allWords.add(wordings[k]);//store the rest tokens
}
for(int m = 0; m < wordings.length; m++)//print
{
System.out.println(wordings[m]);
}
line = br.readLine();
}
Thanks for the help
Jona_T
[2074 byte] By [
Jona_Ta] at [2007-11-27 1:22:58]

Why don't you place some debugging code in, like:
for (int i = 0; i < lineData.length ; i++) {
bex =lineData[i].split("?); //split at "?
}
NumberOfLines.add(bex[0]); //store the first line values in a String Vector
System.out.println("bex[1]: "+bex[1]); // Like this
wordings = bex[1].split(",");// Here is the problem !!
Actually, w/o running it, I think I see what the pblm is - try this:
while ( (line = br.readLine()) != null) {
// lineData = line.split("\n ");
String []wordings = null;
String []bex = null;
// for (int i = 0; i < lineData.length ; i++) {
// bex =lineData[i].split("?);
bex = line.split("?);
NumberOfLines.add(bex[0]);
if ( bex.length > 1 )
wordings = bex[1].split(",");
for (int k = 0; k < wordings.length; k++) {
allWords.add(wordings[k]);
System.out.println(wordings[k]);
}
}
~Bill
Hello Bill,
I tried it out and it printed as usual but gave a nullpointerException Error.
for (int k = 0; k < wordings.length; k++) // Here is the location of the error
{
allWords.add(wordings[k]); //store the rest tokens
System.out.println(wordings[k]);
}
Error Message:
Exception in thread "main" java.lang.NullPointerException
Hello Bill,
It is now working !! and I have learnt something extra. Thanks a lot. The problem of the nullpointerexception is because after the if control-statement , there was no {}. so, it should look like this: if ( bex.length > 1 ){
wordings = bex[1].split(",");
for (int k = 0; k < wordings.length; k++) {
allWords.add(wordings[k]);
System.out.println(wordings[k]);
}
}
Best regards,
Jona_T
> Hello Bill,
> It is now working !! and I have learnt something
> extra. Thanks a lot. The problem of the
> nullpointerexception is because after the if
> control-statement , there was no {}. so, it should
> look like this: [code]
> if ( bex.length > 1 ){
>wordings = bex[1].split(",");
> for (int k = 0; k < wordings.length; k++) {
>allWords.add(wordings[k]);
> System.out.println(wordings[k]);
>}
> /code]
>
> Best regards,
>
> Jona_T
Yes that is so, good catch ... that's what happens a lot of times when the code isn't compiled and run ;o)