Reading a line from a file

A

ataanis

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
 
M

Martin Gerner

Could you please copy-paste your code so that we can look at it?

(e-mail address removed) wrote in @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
 
S

stocksami

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
 
S

stocksami

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();
 
E

Eric Sosman

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.
 
B

Bruce Lee

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
 
B

Bruce Lee

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
that should be empty string will not be added
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top