Reputation: 39
The code below prints all the words from a file(puts it in 1st array) and the number one beside it(second array). If there is a duplicate of the word it finds that word(the very first one) that is in the array and adds 1 to the number array but it still prints out the duplicate in the array. I only want the first instance of the word with the correct number beside it to say how many times in has been in the array. My problem really is i don't want duplicates to be printed out. (No arraylists plz).
while ((in.hasNext())) {
l = in.next() ;
for(int i = 0; i< Wrd.length-1;i++){
if (l.equals(Wrd[i])){
num[i] = num[i] +1;
}
}
Wrd[n]=l;
num[n] = num;
n++;
}
Upvotes: 0
Views: 2165
Reputation: 7179
It sounds like you're not able to use a Set
or Map
etc - if you can then the other suggestions here are much easier to implement what I'm going to suggest :-)
If you cant for some reason, then how about this:
// capture all the words first into an array
// the array below is for test purposes
String[] words = {"1", "2", "3", "5", "1", "1", "3", "4", "1", "5", "7", "0"};
Arrays.sort(words); // sort the array - this is vital or the rest wont work
String last = words[0];
int count = 0;
for (String word : words) {
if (word.equals(last)) {
count++;
} else {
System.out.println(last + "=>" + count);
count = 1;
last = word;
}
}
System.out.println(last + "=>" + count);
The output would be:
0=>1
1=>4
2=>1
3=>2
4=>1
5=>2
7=>1
Upvotes: 1
Reputation: 61148
You need to use a map - this is automatically deal with maintaining a unique list of words. If you override the put
method to aggregate rather than overwrite then it will add up the count automatically.
private void readWords(final Iterator<String> in) {
final Map<String, Integer> wordMap = new HashMap<String, Integer>() {
@Override
public Integer put(String key, Integer value) {
final Integer origValue = get(key);
if (origValue == null) {
return super.put(key, value);
} else {
return super.put(key, origValue + value);
}
}
};
while (in.hasNext()) {
wordMap.put(in.next(), 1);
}
//just for display - not necessary
for (final Entry<String, Integer> entry : wordMap.entrySet()) {
System.out.println("Word '" + entry.getKey() + "' appears " + entry.getValue() + " times.");
}
}
Test:
List<String> strings = new LinkedList<String>();
strings.add("one");
strings.add("two");
strings.add("two");
strings.add("three");
strings.add("three");
strings.add("three");
readWords(strings.iterator());
Output:
Word 'two' appears 2 times.
Word 'one' appears 1 times.
Word 'three' appears 3 times.
You can sort the words alphabetically using a TreeMap
rather than a HashMap
- this may look better for display; depending on what you plan to do with the map.
Upvotes: 0
Reputation: 12243
Track if a given word is a duplicate using a boolean flag and don't add it to the array if it is:
while (in.hasNext()) {
boolean dup = false;
l = in.next() ;
for(int i = 0; i< Wrd.length-1;i++){
if (l.equals(Wrd[i])){
num[i] = num[i] +1;
dup = true;
break; // No reason to check the rest of the array
}
}
if (!dup) {
Wrd[n] = l;
num[n] = num; // If you're looking for frequency, you probably want 1 not num
n++; // only increment the index if we add a new word
}
}
Upvotes: 0