Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2007, 09:22 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
Reading in data from file line by line
I'm working on a project that takes a file filled with lines of integers and I want to take each line and store it into an array.

So for example:

File1.txt


5 6 3 5 4 5 8 7 9
2 6 5 4 9 8 8 6 6
3 1 2 6 5 4 8 9 5
3 3 2 1 5 6 5 8 4




I want to read each line of those integers into an array. So the first line would be its' own array, line 2 would be its' own array, so on and so forth. Assume you know the size of the arrays (in this case let's say nine).

Thoughts Suggestions?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-02-2007, 01:19 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Some ideas. You could read the file, counting the number of lines, make your two-dimensional array with the number of lines. Then read it again and split each line into an array of Strings and parse each one to an int for the line array of ints.
Or you could start with a zero-length array, read the file once, parse each line to ints for the line array and add it to the array.
Or you could read the entire file as a string, count the number of lines and proceed as before.
You could use a BufferedReader or a Scanner for reading the file.
Code:
// Zero-length array int[][] arrayOfInts = new int[0][]; // Split a String String line = reader.readLine(); String[] tokens = line.split("\\s"); You could use a BufferedReader for the above or a Scanner. // Add a line array to the array int len = arrayOfInts.length; int[][] temp = new int[len][]; System.arraycopy(arrayOfInts, 0, temp, 0, len); temp[len] = newLineArray; arrayOfInts = temp; // For a BufferedReader BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(arg)))
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tip for System Defined line separator in a Batch File Java Tip java.lang 0 04-04-2008 03:48 PM
[SOLVED] Delete Current line from file Azndaddy New To Java 1 03-28-2008 01:37 PM
Reading a line from console using Scanner class Java Tip Java Tips 0 01-18-2008 12:52 PM
Reading Data from a file ramachandran New To Java 2 10-24-2007 08:22 AM
Reading file data that contains no spaces jdepue Advanced Java 1 08-01-2007 05:58 AM


All times are GMT +3. The time now is 12:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org