Llist of words and frequency of each word
Write a program to enter text string, then count the number of times each word occurs in that string and print to the screen in the format: word - number of words
output:
Enter string: I love you and you love who I hate you
I: 2
love: 2
you: 3
and:1
who:1
hate:1
Re: Llist of words and frequency of each word
You can't expect us to do your homework. Think of an approach and get started with it.Then we can address your queries.
Re: Llist of words and frequency of each word
ok. thanks
It's my code but it 's not right. Can you have me?
Code Java:
package lab7;
import java.io.*;
public class Main{
private static void linecount(String fName, BufferedReader in)
throws IOException{
long numChar = 0;
long numLine=0;
long numWords = 0;
String line;
do{
line = in.readLine();
if (line != null){
numChar += line.length();
numWords += wordcount(line);
numLine++;
}
}while(line != null);
System.out.println("File Name: " + fName);
System.out.println("Number of characters: " + numChar);
System.out.println("Number of words: " + numWords);
System.out.println("Number of Lines: " + numLine);
}
private static void linecount(String fileName){
BufferedReader in = null;
try{
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
linecount(fileName,in);
}
catch(IOException e){
e.printStackTrace();
}
}
private static long wordcount(String line){
long numWords = 0;
int index = 0;
boolean prevWhiteSpace = true;
while(index < line.length()){
char c = line.charAt(index++);
boolean currWhiteSpace = Character.isWhitespace(c);
if(prevWhiteSpace && !currWhiteSpace){
numWords++;
}
prevWhiteSpace = currWhiteSpace;
}
return numWords;
}
public static void main(String[] args){
long numChar = 0;
long numLine=0;
String line;
try{
if (args.length == 0)
{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
line = in.readLine();
numChar = line.length();
if (numChar != 0){
numLine=1;
}
System.out.println("Number of characters: " + numChar);
System.out.println("Number of words: " + wordcount(line));
System.out.println("Number of lines: " + numLine);
}else{
for(int i = 0; i < args.length; i++){
linecount(args[i]);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
Re: Llist of words and frequency of each word
Does your code compile? If not post the errors.
Does your code execute? If not post the errors.
What does the program output?
Can you show the output and describe what is wrong with it?
Re: Llist of words and frequency of each word
Re: Llist of words and frequency of each word
Quote:
Originally Posted by
Norm
Does your code compile? If not post the errors.
Does your code execute? If not post the errors.
What does the program output?
Can you show the output and describe what is wrong with it?
I compiled this. It compiles without a problem.
Output is:
Quote:
hello mum
Number of characters: 9
Number of words: 2
Number of lines: 1
Not the output I was expecting. Did you write this code jkkj?
Re: Llist of words and frequency of each word
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class HashMapEx {
public static void main(String[] args) {
// Creating new HashMap objects
// keys are String, values are Integer
LinkedHashMap<String, Integer> wordcount = new LinkedHashMap<String, Integer>();
try {
BufferedReader in = new BufferedReader(new FileReader("c:\\test\\kashif.txt"));
// string buffer for file reading
String str;
// reading line by line from file
while ((str = in.readLine()) != null) {
str = str.toLowerCase(); // convert to lower case
// starting index, we'll use this to copy words from string
int idx1 = -1;
// process each characters
for (int i = 0; i < str.length(); i++) {
// trigger condition if current character is not letter
// or it is the end of line
if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) {
// do nothing if previous character was also non-letter
if (i - idx1 > 1) {
// copy word from input string buffer to new variable
// from previous non-letter symbol
// to current symbol which is also non-letter
// if this is a letter(than it is last character in the line
// and we should copy it to word)
if (Character.isLetter(str.charAt(i)))
i++;
// copying...
String word = str.substring(idx1 + 1, i);
// Check if word is in HashMap
if (wordcount.containsKey(word)) {
// get number of occurrences for this word
// increment it
// and put back again
wordcount.put(word, wordcount.get(word) + 1);
} else {
// this is first time we see this word, set value '1'
wordcount.put(word, 1);
}
}
// remember current position as last non-letter symbol
idx1 = i;
}
}
}
// Close buffered reader
in.close();
} catch (Exception e) {
// If something unexpected happened
// print exception information and quit
e.printStackTrace();
System.exit(1);
}
// This code sorts outputs HashMap sorting it by values
// First we're getting values array
ArrayList<Integer> values = new ArrayList<Integer>();
values.addAll(wordcount.values());
// and sorting it (in reverse order)
Collections.sort(values, Collections.reverseOrder());
int last_i = -1;
// Now, for each value
for (Integer i : values) {
if (last_i == i) // without dublicates
continue;
last_i = i;
// we print all hash keys
for (String s : wordcount.keySet()) {
if (wordcount.get(s) == i) // which have this value
System.out.println(s + ":" + i);
}
// pretty inefficient, but works
}
}
}
Re: Llist of words and frequency of each word
Please wrap you code in code tags to preserve its formatting. See: BB Code List - Java Programming Forums
or Go Advanced and use the #icon