Results 1 to 6 of 6
Thread: Read text file and count words.
- 07-17-2011, 02:20 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
Read text file and count words.
Hello
I'm trying to write a program that inputs a text file and counts the words.
Unfortunately, my output isn't counting but displaying only the words with out counting.
My txt input looks like:
Hello I like pizza and java programing.
Sometimes I eat pizza and for breakfast and pizza for lunch
Hello I like pizza and java programing.
Sometimes I eat pizza and for breakfast and pizza for lunch
Hello I like pizza and java programing.
Sometimes I eat pizza and for breakfast and pizza for lunch
Hello I like pizza and java programing.
Sometimes I eat pizza and for breakfast and pizza for lunc
My output looks like:1 and
1 hello
1 java
1 like
1 pizza
1 programing
2 and
1 breakfast
1 eat
2 for
1 lunch
2 pizza
1 sometimes
1 and
1 hello
1 java
1 like
1 pizza
1 programing
2 and
1 breakfast
1 eat
2 for
1 lunch
2 pizza
1 sometimes
1 and
1 hello
1 java
1 like
1 pizza
1 programing
2 and
1 breakfast
1 eat
2 for
1 lunch
2 pizza
1 sometimes
1 and
1 hello
1 java
1 like
1 pizza
1 programing
2 and
1 breakfast
1 eat
2 for
1 lunch
2 pizza
1 sometimes
and my code looks like:
package lab;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class NonDuplicates {
public static void main(String[] args) throws IOException {{
// Create a TreeMap to hold words as key and count as value
Frame f = new Frame();
//decide from where to read the file
FileDialog foBox = new FileDialog(f,"Reading text file", FileDialog.LOAD);
foBox.setVisible(true);
//get the absolute path to the file
String foName = foBox.getFile();
String dirPath = foBox.getDirectory();
// create a file instance for the absolute path
File inFile = new File(dirPath + foName);
BufferedReader in=null;
in= new BufferedReader(new FileReader(inFile));
String line = in.readLine();
while(line!=null)
{
StringTokenizer t = new StringTokenizer(line,"|");
{
String id = t.nextToken().trim();
// read in the next line
line=in.readLine();
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
String[] words = id.split("[ \n\t\r.,;:!?(){}]");
for (int i = 0; i < words.length; i++) {
String key = words[i].toLowerCase();
if (words[i].length() > 1) {
if (map.get(key) == null) {
map.put(key, 1);
}
else {
int value = map.get(key).intValue();
value++;
map.put(key, value++);
}
}
}
// Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// Get key and value from each entry
for (Map.Entry<String, Integer> entry: entrySet)
System.out.println(entry.getValue() + "\t" + entry.getKey());
}
}
}
}
}
Where should I start to look??
Thanks
- 07-17-2011, 02:45 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I am not sure I understand this - the output you posted does show word counts. What output were you expecting from that input?I'm trying to write a program that inputs a text file and counts the words.
Unfortunately, my output isn't counting but displaying only the words with out counting.
In fact the input you posted is not the actual intput that producd that output (there should also be lines which set the id). It would be a good idea to use a String to represent the input data rather than reading it from a file: that way we can all see the data that is being used. Once the logic of your program is correct you can add the code that reads the data from a file.
-----
Notice that you create a new counting map each time you read a line:
The effect of this is that map will only have word counts for that particular line. If you want to accumulate word counts then map should be initialised outside (before) the while loop and its contents printed after that loop.Java Code:line = in.readLine(); TreeMap<String, Integer> map = new TreeMap<String, Integer>();
- 07-17-2011, 03:45 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
@op: a few suggestions, first, don't use StringTokenizer, it's a legacy class and should not be used in new code. Instead, use split more. Split the line based on | and then loopthrough the produced array and do all the parsing in the loop.
You are also doing unnecessary setup for in.readLine(), instead of
DoJava Code:read line while read line
Java Code:String s; while((s = in.readLine())!= null) parse line
Last edited by sunde887; 07-17-2011 at 02:33 PM.
- 07-17-2011, 02:26 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 07-17-2011, 02:32 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Last edited by sunde887; 07-17-2011 at 02:35 PM.
- 07-17-2011, 02:44 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Similar Threads
-
Count the frequency of the word in a text file instead of a sentence.
By bMorgan in forum New To JavaReplies: 3Last Post: 11-09-2010, 12:10 AM -
Read in words from a text file that contains a story
By Pleenen in forum Advanced JavaReplies: 2Last Post: 05-27-2010, 04:34 AM -
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Using Scanner to count paragraphs in a text file.
By Vox in forum New To JavaReplies: 3Last Post: 03-22-2010, 07:27 PM -
[SOLVED] How to count the number of words in a string
By andy5605 in forum New To JavaReplies: 8Last Post: 02-04-2009, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
I'm sorry, I didn't know... there should be a law against deleting non-duplicate posts

Bookmarks