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]
# 1

Shouldn't

or (int k = 0; k < allWords.size(); k++)

{

allWords.add(wordings[k]); //store the rest tokens

}

be

or (int k = 0; k < wordings.length; k++) // chg

{

allWords.add(wordings[k]); //store the rest tokens

}

abillconsla at 2007-7-12 0:10:54 > top of Java-index,Java Essentials,New To Java...
# 2
Yes, you are correct but the problem is still there. On compiling, it prints all the tokens correctly and also "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1"Jona_T
Jona_Ta at 2007-7-12 0:10:54 > top of Java-index,Java Essentials,New To Java...
# 3

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 !!

abillconsla at 2007-7-12 0:10:54 > top of Java-index,Java Essentials,New To Java...
# 4

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

abillconsla at 2007-7-12 0:10:54 > top of Java-index,Java Essentials,New To Java...
# 5

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

Jona_Ta at 2007-7-12 0:10:55 > top of Java-index,Java Essentials,New To Java...
# 6

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

Jona_Ta at 2007-7-12 0:10:55 > top of Java-index,Java Essentials,New To Java...
# 7
never mind (deleted)Message was edited by: rym82
rym82a at 2007-7-12 0:10:55 > top of Java-index,Java Essentials,New To Java...
# 8

> 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)

abillconsla at 2007-7-12 0:10:55 > top of Java-index,Java Essentials,New To Java...