0

I am trying to count the amount of times a word is repeated in stdin.

Example input:

This is a test, this is is

Desired output:

this 2
is   3 
a    1
test 1

I have an int[] to store the wordCount but I'm not sure where to use it, the int count is just temporary so the program can run.

Here is my code for reference:

import java.util.Scanner;
public class  WCount {

    public static void main (String[] args) {

        Scanner stdin = new Scanner(System.in);

        String [] wordArray = new String [10000];
        int [] wordCount = new int [10000];
        int numWords = 0;

        while(stdin.hasNextLine()){
            String s = stdin.nextLine();
            String [] words =  s.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\\
s+"); //stores strings as words after converting to lowercase and getting rid of punctuation 
            for(int i = 0; i < words.length; i++){
        int count = 0; //temporary so program can run
            for(int  j = 0; j < words.length; j++){
            if( words[i] == words[j] )
        count++;
                    System.out.println("word count: → " + words[i] + " " +  count);
}
               }

    }
Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37
java_guy
  • 75
  • 1
  • 7
  • I think you have the wrong number of backslashes in the string literal you pass to `split`. It's hard to tell, though, because the line got wrapped and I don't know whether the last backslash is really part of your source. Please post the exact text of the `split` call, and in a way that doesn't wrap. – ajb Feb 18 '16 at 07:12
  • Also see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. Normally I'd just close this question as a duplicate of that, but there are other interesting points that need addressing. – ajb Feb 18 '16 at 07:14

5 Answers5

2

I would use something like this:

import java.util.ArrayList;
import java.util.Scanner;

public class WCount  {

    public static void main(String[] args) {

        Scanner stdin = new Scanner(System.in);

        String[] wordArray = new String[10000];
        int[] wordCount = new int[10000];
        int numWords = 0;           

        while (stdin.hasNextLine()) {
            String s = stdin.nextLine();
            ArrayList<String> noDuplicated = new ArrayList<String>();
            String[] words = s.replaceAll("[^a-zA-Z ]", "").toLowerCase()
                    .split("\\s+"); // stores strings as words after converting
                                    // to lowercase and getting rid of
                                    // punctuation

            //Array that contains the words without the duplicates ones
            for (int i = 0; i < words.length; i++) {
                if(!noDuplicated.contains(words[i]))
                    noDuplicated.add(words[i]); 
            }

            //Count and print the words
            for(int i=0; i<noDuplicated.size();i++){
                int count = 0;
                for (int j = 0; j < words.length; j++) {
                    if (noDuplicated.get(i).equals(words[j]))
                        count++;                    
                }
                System.out.println("word count: → " + words[i] + " "
                            + count);
            }

        }
    }
}

output:

This is a test, this is is
word count: → this 2
word count: → is 3
word count: → a 1
word count: → test 1

Hope it is usefull!

tumisma
  • 375
  • 3
  • 14
1

This works for me. Although iterating through the complete possible array is silly. It would work easier with ArrayList. But as I am not sure if you are allowed to use it.

    import java.util.Scanner;

    public class WCount {

        public static void main(String[] args) {

            Scanner stdin = new Scanner(System.in);

            int[] wordCount = new int[1000];
            String[] wordList = new String[1000];

            int j = 0;
            while (stdin.hasNextLine()) {
                String s = stdin.nextLine();
                String[] words = s.split("\\W+");

                for (String word : words) {

                    int listIndex = -1;
                    for (int i = 0; i < wordList.length; i++) {
                        if (word.equals(wordList[i])) {
                            listIndex = i;
                        }
                    }

                    if (listIndex > -1) {
                        wordCount[listIndex]++;
                    } else {
                        wordList[j] = word;
                        wordCount[j]++;
                        j++;
                    }

                }

                for (int i = 0; i < j; i++) {
                    System.out.println("the word: " + wordList[i] + " occured " + wordCount[i] + " time(s).");
                }
            }
        }

    }

output:

this is a test. this is cool.
the word: this occured 2 time(s).
the word: is occured 2 time(s).
the word: a occured 1 time(s).
the word: test occured 1 time(s).
the word: cool occured 1 time(s).
blaster
  • 845
  • 1
  • 10
  • 25
0

You can use a hash table here. I am not going to write code for you, but here is simple pseudo algorithm:

if hashTable contains word 
     hashTable.put(word, words.value + 1)
else hashTable.put(word, 1)

Do this for each word in the word array. After all the words have been handled, you simply print each key (word) in the hash table with its value (number of times it appeared).

Hope this helps!

0

figured this out...simpler way..

import java.util.Vector;

public class Get{
    public static void main(String[]args){

        Vector<String> ch = new Vector<String>();
        String len[] = {"this", "is", "this", "test", "is", "a", "is"};
        int count;

        for(int i=0; i<len.length; i++){
            count=0;
            for(int j=0; j<len.length; j++){
                if(len[i]==len[j]){
                    count++;
                }
            }

            if(count>0){
                if(!ch.contains(len[i])){
                    System.out.println(len[i] + " - " + count);
                    ch.add(len[i]);
                }
            }
        }
    }
}

Output:
this - 2
is - 3
test - 1
a - 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
Tobi
  • 1
  • 2
0

My implementation:

public static void main(final String[] args) {

    try (Scanner stdin = new Scanner(System.in)) {

        while (stdin.hasNextLine()) {
            Map<String, AtomicInteger> termCounts = new HashMap<>();
            String s = stdin.nextLine();
            String[] words = s.toLowerCase().split("[^a-zA-Z]+");

            for (String word : words) {
                AtomicInteger termCount = termCounts.get(word);
                if (termCount == null) {
                    termCount = new AtomicInteger();
                    termCounts.put(word, termCount);
                }
                termCount.incrementAndGet();
            }

            for (Entry<String, AtomicInteger> termCount : termCounts.entrySet()) {
                System.out.println("word count: " + termCount.getKey() + " " + termCount.getValue());
            }
        }

    }
}
roblovelock
  • 1,971
  • 2
  • 23
  • 41