0

I wrote a code that reads all words from a text, counts all the unique words, and writes a new array with all the unique words and the number of times that word is repeated on the text. For one reason when I execute it, the program treats all the words as unique and on the "if" loop the condition becomes "false" for all the words. Do you know what should I change from my code to make it compare the words properly? Thanks!

import java.util.*;


class textAnalyzer{

public static void main(String[] args){

    Help hj = new Help();
    hj.metode1(args[0]);
}
}


class Help{
void metode1(String filename){

    In les = new In (filname); //input *.txt file

    int totalWords = 0; // counter with total words from the text
    int uniqueW = 0; //counter with the number of total unique words
    boolean funnet = false;

    String[] word = new String[30835]; //array with each unique word
    int quantity[] = new int[30835]; // array the number of times a unique word is repeated on the text

    while(read.endOfFile() == false) {


        for(int i = 0; i < word.length; i++){
                        String oneWord = read.inWord();
                        totalWords++;

            if(ord[i] == denneOrd){
                found = true;
            }

            if(found){
                quantity[i]++;
                uniqueW++;
            }else{
                word[i] = oneWord;
                }   

        }

        totalWords++
    }

    System.out.println("Number words read: " + totalWords + " number unique words: " + uniqueW);



}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Side note for string comparision : `if(ord[i].equals(denneOrd))` [why-doesnt ==-work-on-string](http://stackoverflow.com/questions/17443201/why-doesnt-work-on-string/17443215#17443215) – Suresh Atta Oct 16 '13 at 10:47
  • If you want a library that does this, look at Guava (e.g. Multiset). This and similar classes will count all the unique occurrences – peter.murray.rust Oct 16 '13 at 10:54
  • *"JAVA. Program to read.."* 1) It is spelled 'Java', not 'JAVA' 2) Don't include the major tag in the title. – Andrew Thompson Oct 16 '13 at 10:55

4 Answers4

0

use "==" to compare for primitive data type, For Objects you can use equals method to compare with other objects. For String objects you can use .equalsIgnoreCas() method to ignore the case.

if(word[i] == denneOrd){   //here you need to change 
    found = true;
}

need change to

if(word[i].equals(denneOrd)){ 
    found = true;
}
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

use .equals method or .equalsIgnoreCase if you don't want to consider case, instead of ==.

  if(ord[i].equals(denneOrd)){
                found = true;
            }

or

  if(ord[i].equalsIgnoreCase(denneOrd)){
                found = true;
            }
X-Pippes
  • 1,170
  • 7
  • 25
0

put all the words in a set and then check the size of a set

How to add an item to a set

and then check the sise of set using

checking size of set

Set has the property of removing duplicates

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You'd better use to use an HashMap for this task:

Map<String, Integer> word_counts = new HashMap<String, Integer>();

for (Strign word : words_producer) { 

    Integer count = word_counts.get(word, 0);
    word_counts.put(word, (count == null) ? 1 : count + 1);
}

// Get the set of unique words
Set<String> words = word_counts.keySet();

// Print each word's count
for (String word : words) {
    int count = word_counts.get(word);
    System.out.printf("word: %s, count: %d\n", word, count);
}
user278064
  • 9,982
  • 1
  • 33
  • 46