Results 1 to 6 of 6
  1. #1
    szimme101 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Question 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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:
    Java 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
    Java Code:
    Toyota
    Subaru
    Volkswagen
    Chevrolet
    Ford
    Chrysler
    Dodge
    For info on array copying see the bottom of this page: Arrays

  3. #3
    szimme101 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Smile 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:

    Java 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?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

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

  5. #5
    adarsh_2484 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Smile Hello

    Hi,

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

    With Regards
    Adarsh

  6. #6
    ipodfansmail is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 07:43 AM
  2. Replies: 16
    Last Post: 05-08-2008, 10:45 PM
  3. Replies: 3
    Last Post: 04-07-2008, 07:02 PM
  4. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •