Results 1 to 6 of 6
- 03-23-2008, 05:44 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
- 03-23-2008, 08:02 PM #2
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:
input.txtJava 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]); } }
For info on array copying see the bottom of this page: ArraysJava Code:Toyota Subaru Volkswagen Chevrolet Ford Chrysler Dodge
- 03-23-2008, 09:00 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
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:
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?Java Code:Toyota Camry Subaru Forester Volkswagen Gti Chevrolet Malibu Ford Focus Chrysler Crossfire Dodge Charger
- 03-23-2008, 09:29 PM #4
Java 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(); } } } }
- 07-24-2008, 08:17 AM #5
Member
- Join Date
- Jul 2008
- Posts
- 5
- Rep Power
- 0
Hello
Hi,
Hello to all.I am new in java.I wish every one would help me to clear my doubts.
With Regards
Adarsh
- 07-30-2008, 09:30 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Need a solution to read and store data from a file
By sheetalnri in forum New To JavaReplies: 10Last Post: 09-30-2010, 06:43 AM -
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 09:45 PM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks