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 03-23-2008, 06:44 PM
Member
 
Join Date: Mar 2008
Posts: 4
szimme101 is on a distinguished road
How do you read from a file, and then store the info in an array?
Here is my problem:

I am going to have to use a bufferedReader to read a file containing information on 10 vehicles. And after I read that file I want to store the info in Vehicle [] V.


Where should I start?

Thanks

Steve
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-23-2008, 09:02 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
There are a number of ways you can go about this.
One is to declare an array of type String large enough to be certain that you will have enough space for the entire file. So if the file has 20 lines in it and you use one array element for each line you could declare the array length to be 100 or 1000. Then after reading the file and assigning each line (read from the file) to an array element you can copy the data from the big array into a new array that is the length that you need, ie, the length is the number of lines you read from the file.
Another is to read the file two times: once to count the lines and again filling an array declared and allocated to be the same length as the number of lines counted.
Another is really easy — use an ArrayList and add each line as you read. Then to convert it to an array use the List method toArray(new String[list.size()].
Another is to add each line to the array as you read — demonstrated here:
Code:
import java.io.*; public class InputTest { public static void main(String[] args) { String[] lines = new String[0]; String path = "input.txt"; BufferedReader br = null; try { File file = new File(path); br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); String line; while( (line = br.readLine()) != null ) { lines = add(line, lines); } br.close(); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } print(lines); } private static String[] add(String s, String[] array) { int len = array.length; String[] temp = new String[len+1]; System.arraycopy(array, 0, temp, 0, len); temp[len] = s; return temp; } private static void print(String[] data) { for(int i = 0; i < data.length; i++) System.out.println(data[i]); } }
input.txt
Code:
Toyota Subaru Volkswagen Chevrolet Ford Chrysler Dodge
For info on array copying see the bottom of this page: Arrays
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-23-2008, 10:00 PM
Member
 
Join Date: Mar 2008
Posts: 4
szimme101 is on a distinguished road
Changing the input file and utilizing split()
I understand your code and it works well on my computer but now I need to change it alittle.
This imput file will look more like:

Code:
Toyota Camry Subaru Forester Volkswagen Gti Chevrolet Malibu Ford Focus Chrysler Crossfire Dodge Charger
I want to use split(" ") but I dont knowwhere it would be put in your code, could you show me how to use string splitting?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-23-2008, 10:29 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Code:
private static void print(String[] data) { for(int i = 0; i < data.length; i++) { String line = data[i]; String[] words = line.split("\\s"); for(int j = 0; j < words.length; j++) { System.out.print(words[j]); if(j < words.length-1) System.out.print(", "); else System.out.println(); } } } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-24-2008, 09:17 AM
Member
 
Join Date: Jul 2008
Posts: 5
adarsh_2484 is on a distinguished road
Hello
Hi,

Hello to all.I am new in java.I wish every one would help me to clear my doubts.

With Regards
Adarsh
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-30-2008, 10:30 AM
Member
 
Join Date: Jul 2008
Posts: 1
ipodfansmail is on a distinguished road
reply
I can't agree with you more.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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
Beginner; Create a class to store info and constructor to initialize badness New To Java 16 05-08-2008 10:45 PM
How would you get information from a file and then store it in an array? szimme101 Advanced Java 3 04-07-2008 07:02 PM
[SOLVED] How to read a file and compare Array values DonCash Advanced Java 2 04-02-2008 03:22 PM
Need a solution to read and store data from a file sheetalnri New To Java 3 12-28-2007 10:35 PM


All times are GMT +3. The time now is 06:46 PM.


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