Results 1 to 5 of 5
Thread: Reading a file into an array?
- 07-07-2011, 12:56 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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());
}
}
- 07-07-2011, 12:59 AM #2
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.
- 07-07-2011, 01:06 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 07-07-2011, 04:08 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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.
- 07-07-2011, 04:22 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
This can be made more compact with thisJava Code:loop read line Do stuff with line end loop
Java Code:while the read in line isn't null do something with the line end while
Similar Threads
-
Help with GUI, Array, and Reading File
By bamagirl31 in forum New To JavaReplies: 21Last Post: 07-05-2011, 01:14 AM -
Help with reading file into array
By xkillswitchx14 in forum New To JavaReplies: 2Last Post: 04-28-2011, 10:24 PM -
Reading csv file into 2D array - HELP!!!
By mikeg in forum New To JavaReplies: 17Last Post: 04-12-2011, 08:36 AM -
Help with reading from file into an array
By Trad in forum New To JavaReplies: 3Last Post: 10-22-2010, 12:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks