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
Printable View
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
We don't write code for you here. Make an effort and we will help(hint: a Map<String, Integer> will be very helpful).
This 's my code. but it 's not right.
I want output:
example:
Enter string: I am a student a student not good.
I: 1
am: 1
a: 2
Student: 2
not: 1
good: 1
Code:
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();
}
}
}
I don't see how your code is supposed to do anything to what you asked. This doesn't keep track of what words have been used at all...
What kind of techniques and/or classes do you know how to use to be able to remember the counts?Quote:
count the number of times each word occurs in that string
Cross posted at Llist of words and frequency of each word
Use as sunde887 suggested Map<String, Integer> .It s the best and easiest way to do it. Try it out.
Hi,
1) Counts the No of Words in a String
2) Place each of those words in a buffer (means any Array, List r some Storage)
3) Iterate through that Buffer Starting with the first word
4) Count the occurrence of the word
just think like a student solving a problem ......... u can easily build upon the logic