Results 1 to 5 of 5
  1. #1
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default Reading a file into an array?

    Hello,
    I need to read a file into an array so I am able to manipulate it later. Among many things, taking a count of all the words in the file is something I need to do. My program is reading a file and storing as an array. It gives me a wrong number though. It only reads words up until a return/paragraph in the file. Any help on this would be much appreciated.
    Here is the code:


    import java.util.*;
    import java.io.*;
    import java.lang.*;
    import java.text.*;

    public class fileReader
    {
    public static String my_fName, str1 = null;
    public static String[] words;

    public fileReader(String fName)
    {
    try{
    my_fName = fName;
    FileInputStream fstream = new FileInputStream(my_fName);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader (new InputStreamReader(in));

    (str1 = br.readLine()

    String [] tokens = str1.split(" ");
    words = tokens;


    }
    catch (Exception e){}
    }

    public static int numWords() throws NullPointerException
    {
    //String [] tokens = str1.split(" ");
    int num1 = words.length;
    return num1;
    }

    public static void main (String []args)
    {
    fileReader fr1 = new fileReader (args[0]);
    System.out.println(fr1.numWords());
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    As far as I can see you only read one line from the file. You will need a loop to keep on reading until it gets to the end of the file.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As junky said, you will need a loop. Also, it may be easier to use an array list, which can resize, as opposed to an array which has a fixed size.

    When posting code, please use code tags
    [code]
    YOUR CODE HERE
    [/code]

  4. #4
    heycoa is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    is there any way you could give me an example? I am pretty new to java so that would be appreciated. The reason im not using an array list is because the array length will not need to be manipulated.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The reason I suggested an ArrayList is because it can read the file in regardless of size, with an array you will need to know how many elements to allocate upon declaration of the array. You can calculate the size of the file before declaring and initializing the array but you will effectively be looping trough the file twice.

    Here is pseudo code
    Java Code:
    loop read line
      Do stuff with line
    end loop
    This can be made more compact with this

    Java Code:
    while the read in line isn't null
      do something with the line 
    end while

Similar Threads

  1. Help with GUI, Array, and Reading File
    By bamagirl31 in forum New To Java
    Replies: 21
    Last Post: 07-05-2011, 02:14 AM
  2. Help with reading file into array
    By xkillswitchx14 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 11:24 PM
  3. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 09:36 AM
  4. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 01:16 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
  •