user12682007
user12682007

Reputation:

Counting the distinct words

how could I count the distinct words in a text that spans several lines?

Input data: You will read the text of the email from the keyboard. It can span multiple lines and contains only lowercase letters of the English alphabet and spaces.

Output data: A single integer representing the number of distinct words in the email will be displayed.

I have this code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String args[] ) throws Exception {
    Set<String> words = new TreeSet<>();
    int count = 0;
    try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
        String line;
        while((line = reader.readLine()) != null){
            words.add(line);
            count++;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println(count);
 }

Basically it only works on a test, but on the rest yes it is wrong, since I understand it is because it still reads an empty space, what could I repair?

Upvotes: 0

Views: 199

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201467

Change

words.add(line);

to add the words from each line. Split on white space, and add the resulting tokens. Like,

words.addAll(Arrays.asList(line.split("\\s+")));

And then change

System.out.println(count);

to

System.out.println(words.size());

Finally, eliminate count (you don't need it, and the Set "counts" the elements by the number of elements with-in the set; i.e. words.size()). And, unless there's some reason to order your elements, use a HashSet.

Set<String> words = new TreeSet<>();

Should be (as far as I can tell)

Set<String> words = new HashSet<>();

Upvotes: 2

Related Questions