Java Programming - Obtaining Information From a File
I am trying to write a program which gets a segment of text from a file and tells how many times particular words appeared in the text. The file where the text comes from is set up like this:
A common application
in many areas of data processing
involves searching the contents of a file
for occurrences
of a particular word.
end_of_data
processing
of
word
end_of_data
The first text before the "end_of_data" is the text that is to be searched. The text that is after the "end_of_data" and before the next end_of_data are the words that you are looking for in the text.
Like in this example, the program I am writing would return something like this:
processing appears in line(s) 2 a total of 1 time(s)
of appears in line(s) 2, 3, 5 a total of 3 time(s)
word appears in line(s) 5 a total of 1 time(s)
I have the first part of the text kept in an ArrayList using a loop like this:
publicstaticvoid main(String[] args)throws IOException
{
ArrayList strList =new ArrayList();
Fill(strList);
System.out.println(strList);
}
publicstaticvoid Fill(ArrayList list)throws IOException
{
Scanner input =new Scanner(new File("data.txt"));
String text = input.nextLine();
String end ="end_of_data";
while(text.compareTo(end)!= 0)
{
list.add(text);
text = input.nextLine();
}
}
How should I go about getting the search words out of the file? I've been trying to figure out some kind of loop to get them out but I can't seem to come up with anything.
Thanks in advance. :)
[2385 byte] By [
Bethya] at [2007-11-26 23:26:09]

here is one really, really ugly way to do it.
import java.io.BufferedReader;import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class FindWords {
private static Properties wordCount = new Properties();
private static Properties lineCount = new Properties();
public static void main(String[] args) {
loadSearchTerms();
searchForWords();
Enumeration keys = wordCount.keys();
while (keys.hasMoreElements()) {
String str = (String)keys.nextElement();
System.out.println(
str + " appears in lines " + lineCount.getProperty(str) +
" a total of " + wordCount.getProperty(str) + " times"
);
}
}
private static void loadSearchTerms() {
try {
BufferedReader in = new BufferedReader(new FileReader("words.txt"));
String str;
boolean startReading = false;
while ((str = in.readLine()) != null) {
if ("end_of_data".equals(str)) startReading = !startReading;
if ((!startReading)) continue;
if (!"end_of_data".equals(str)) wordCount.setProperty(str, "0");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void searchForWords() {
try {
BufferedReader in = new BufferedReader(new FileReader("words.txt"));
String str;
int lineCounter = 1;
while ((str = in.readLine()) != null && !"end_of_data".equals(str)) {
Enumeration keys = wordCount.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (str.contains(key)) {
Integer newCount = Integer.parseInt(wordCount.getProperty(key)) + 1;
String lines = lineCount.getProperty(key, "");
wordCount.setProperty(key, newCount.toString());
lineCount.setProperty(key, lineCounter + "," + lines);
}
}
lineCounter++;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}