Java String array and Split question

M

Mullin

.....
.....
lineBuf = r.readLine();
String[] results = lineBuf.split(",");
.....
.....

if the line at the text file is
case 1
======
1,named,phoned,field1d,,,
=> results.length = 4

case 2
=======
2,namec,phonec,field1c,field2c,field3c,field4c
=> results.length = 7

case 3
1,named,phoned,field1d,,,
=> results.length = 7
note: there's a white space at the end of case 3

i find it strange that for case 1, i expect a SEVEN-element array will
be create/returned, but case 1 only return a 4-element array to me.
 
D

Dotty

Mullin said:
....
....
lineBuf = r.readLine();
String[] results = lineBuf.split(",");
....
....

if the line at the text file is
case 1
======
1,named,phoned,field1d,,,
=> results.length = 4

case 2
=======
2,namec,phonec,field1c,field2c,field3c,field4c
=> results.length = 7

case 3
1,named,phoned,field1d,,,
=> results.length = 7
note: there's a white space at the end of case 3

i find it strange that for case 1, i expect a SEVEN-element array will
be create/returned, but case 1 only return a 4-element array to me.


Comment in file "String.java"

the pattern will be applied as many times as possible, the array can
have any length, and trailing empty strings will be discarded.
 
A

Alan Moore

....
....
lineBuf = r.readLine();
String[] results = lineBuf.split(",");
....
....

if the line at the text file is
case 1
======
1,named,phoned,field1d,,,
=> results.length = 4

case 2
=======
2,namec,phonec,field1c,field2c,field3c,field4c
=> results.length = 7

case 3
1,named,phoned,field1d,,,
=> results.length = 7
note: there's a white space at the end of case 3

i find it strange that for case 1, i expect a SEVEN-element array will
be create/returned, but case 1 only return a 4-element array to me.


String[] results = lineBuf.split(",", -1);
 
N

Nigel Wade

Mullin said:
....
....
lineBuf = r.readLine();
String[] results = lineBuf.split(",");
....
....

if the line at the text file is
case 1
======
1,named,phoned,field1d,,,
=> results.length = 4

case 2
=======
2,namec,phonec,field1c,field2c,field3c,field4c
=> results.length = 7

case 3
1,named,phoned,field1d,,,
=> results.length = 7
note: there's a white space at the end of case 3

i find it strange that for case 1, i expect a SEVEN-element array will
be create/returned, but case 1 only return a 4-element array to me.


It's only strange to those who haven't read the documentation. To *expect* a
particular result from a method you don't know is foolhardy. Reading the
documentation would have told you to expect what you actually got.

RTFM at:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
and note the 2-argument split method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top