Reading a line from a file  
Author Message
ataanis





PostPosted: 2006-5-5 1:21:00 Top

java-programmer, Reading a line from a file Hi to all, I'm trying to read from a file, line by line, and parse out
the data, by using the line.split(" ") function, and I have a file that
has 8 entries total per line, which means that it supposed to read up
to , array[7], but for some reason, when I run it , it says array
outofboundaries. The only thing here that is to notice, is that the
value at 7 might be either a string or an empty string, can that be a
problem?
Thanks

 
Martin Gerner





PostPosted: 2006-5-5 2:10:00 Top

java-programmer >> Reading a line from a file Could you please copy-paste your code so that we can look at it?

email***@***.com wrote in news:1146763242.632744.176950
@j73g2000cwa.googlegroups.com:

> Hi to all, I'm trying to read from a file, line by line, and parse out
> the data, by using the line.split(" ") function, and I have a file that
> has 8 entries total per line, which means that it supposed to read up
> to , array[7], but for some reason, when I run it , it says array
> outofboundaries. The only thing here that is to notice, is that the
> value at 7 might be either a string or an empty string, can that be a
> problem?
> Thanks


--
Martin Gerner
 
stocksami





PostPosted: 2006-5-5 2:22:00 Top

java-programmer >> Reading a line from a file
email***@***.com wrote:
> Hi to all, I'm trying to read from a file, line by line, and parse out
> the data, by using the line.split(" ") function, and I have a file that
> has 8 entries total per line, which means that it supposed to read up
> to , array[7], but for some reason, when I run it , it says array
> outofboundaries. The only thing here that is to notice, is that the
> value at 7 might be either a string or an empty string, can that be a
> problem?
> Thanks

Try using the trim method of the readLine before using split.

example:

String s = in.readLine.trim();

where in is a BufferedReader

 
 
stocksami





PostPosted: 2006-5-5 2:25:00 Top

java-programmer >> Reading a line from a file
email***@***.com wrote:
> email***@***.com wrote:
> > Hi to all, I'm trying to read from a file, line by line, and parse out
> > the data, by using the line.split(" ") function, and I have a file that
> > has 8 entries total per line, which means that it supposed to read up
> > to , array[7], but for some reason, when I run it , it says array
> > outofboundaries. The only thing here that is to notice, is that the
> > value at 7 might be either a string or an empty string, can that be a
> > problem?
> > Thanks
>
> Try using the trim method of the readLine before using split.
>
> example:
>
> String s = in.readLine.trim();
>
> where in is a BufferedReader

Whoops,

should have been
String s = in.readLine().trim();

 
 
Eric Sosman





PostPosted: 2006-5-5 3:00:00 Top

java-programmer >> Reading a line from a file

email***@***.com wrote On 05/04/06 13:20,:
> Hi to all, I'm trying to read from a file, line by line, and parse out
> the data, by using the line.split(" ") function, and I have a file that
> has 8 entries total per line, which means that it supposed to read up
> to , array[7], but for some reason, when I run it , it says array
> outofboundaries. The only thing here that is to notice, is that the
> value at 7 might be either a string or an empty string, can that be a
> problem?

Yes: You are using a form of split() that discards
trailing empty strings; see the Javadoc. There's a two-
argument split() method that can be used to retain the
empty trailers; again, see the Javadoc.

--
email***@***.com

 
 
Bruce Lee





PostPosted: 2006-5-5 3:11:00 Top

java-programmer >> Reading a line from a file
<email***@***.com> wrote in message
news:email***@***.com...
> Hi to all, I'm trying to read from a file, line by line, and parse out
> the data, by using the line.split(" ") function, and I have a file that
> has 8 entries total per line, which means that it supposed to read up
> to , array[7], but for some reason, when I run it , it says array
> outofboundaries. The only thing here that is to notice, is that the
> value at 7 might be either a string or an empty string, can that be a
> problem?
> Thanks
>

yep. an empty line will not be added