Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Reading a line from a file

Thread view: 
ataanis@gmail.com - 04 May 2006 18:20 GMT
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 - 04 May 2006 19:10 GMT
Could you please copy-paste your code so that we can look at it?

ataanis@gmail.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
[quoted text clipped - 4 lines]
> problem?
> Thanks

Signature

Martin Gerner

stocksami@earthlink.net - 04 May 2006 19:22 GMT
> 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
[quoted text clipped - 4 lines]
> 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@earthlink.net - 04 May 2006 19:25 GMT
stocks...@earthlink.net 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
[quoted text clipped - 12 lines]
>
> where in is a BufferedReader

Whoops,

should have been
String s = in.readLine().trim();
Eric Sosman - 04 May 2006 20:00 GMT
ataanis@gmail.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
[quoted text clipped - 3 lines]
> 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.

Signature

Eric.Sosman@sun.com

Bruce Lee - 04 May 2006 20:10 GMT
> 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
[quoted text clipped - 4 lines]
> problem?
> Thanks

yep. an empty line will not be added
Bruce Lee - 04 May 2006 20:11 GMT
> 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
[quoted text clipped - 4 lines]
> problem?
> Thanks

that should be empty string will not be added


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.