-1

I did wrote a code that gets a text file as input, then the program makes to equal String arrays with the text so the words can be compared and find + count the unique and repeated words. The program becomes compiled successfully but if I try to execute it so I get the problem: Exception in thread"main"java.lang.NullPointerException...java:52. The problem must be in the way I declare the strings. How should I write them instead? Thanks!

import java.util.*;

class TextAnalyze{
public static void main(String[] args){
In read = new In ("text1.txt"); //input *.txt file

    int tWords = 0; // counter, total words in the text file
    int unW = 0; //counter UNIQUE words in the text file
    String[] word = new String[31000]; //array with all the words
    String[] word2 = new String[31000]; // array with all the words, used to compare
    //String uniqueWords[] = new String[31000]; //array with the unique words
    //int numberuniqueWords[] = new int [31000];

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

        word[tWords] = read.inWord();
        word2[tWords] = word[tWords];
        tWords++;

    }

    int totalWords = word.length;
    int totalWords2 = word2.length;

    List<String> uniqueWords = new ArrayList<>();

    for (int i = 0; i < totalWords; i++) { // loop of the first array list
        boolean unique = true;

        for (int j = 0; j < totalWords2; j++) { // second loop where the
                                        // words are being compared
            if (word[i].equals(word2[j])) {
                //we find two equals strings, it not unique
                unique = false;
                break;
            }
        }

        //if it remains unique there wasn't equals
        if (unique) {
            uniqueWords.add(word[i]);
        }
    }

    for (String s : uniqueWords) {
        System.out.println(s);
    }

}

}

Filburt
  • 17,626
  • 12
  • 64
  • 115

4 Answers4

0
int totalWords = word.length;

This is probably your problem. The length of an array is it's capacity, not the actual number of objects placed in it. So that will always return 31000. If that is not the number of words in your text file, then your loop will be pulling null values from your array.

Aurand
  • 5,487
  • 1
  • 25
  • 35
0

I think @Aurand has identified the problem. You may now put a null check over your comparison :

if(word[i] != null && word2[j] != null){
            if (word[i].equals(word2[j])) {
                //we find two equals strings, it not unique
                unique = false;
                break;
            }
}
Crickcoder
  • 2,135
  • 4
  • 22
  • 36
  • or just use tWords instead of word.length – Xonar Oct 11 '13 at 06:09
  • Personally, I'd put the null check in the loop condition. Due to the way the array is populated, there will be no values beyond the first null. – Aurand Oct 11 '13 at 06:22
  • @Aurand : Agree with that. There should be a break in the else part. Although I don't feel that the user is worried about performance. – Crickcoder Oct 11 '13 at 06:26
0
        FileReader fr = new FileReader("/home/rajesh/Desktop/movie");
        BufferedReader br = new BufferedReader(fr);
        String s;
        Set<String> sdata = new HashSet<String>();
        List<String> adata = new ArrayList<String>();
        while ((s = br.readLine()) != null) {
            for (String val : s.split(" ")) {
                sdata.add(val);
                adata.add(val);
            }
        }

        for (String val : sdata) {    
            int freq = Collections.frequency(adata, val);
            System.out.println("Frequency of " + val + " " + freq);
        }

If you are going to run this code in command mode you have to specify try catch block or throws.

0
import java.util.Vector;
import java.util.Scanner;

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

Scanner in = new Scanner(System.in);    
System.out.print("enter a text? ");
String txt = in.nextLine();

Vector<Character> ch = new Vector<Character>();
char len[] = txt.toCharArray();
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]);
    }
    }
}


}
}
Tobi
  • 1
  • 2
  • Don't simply paste code, explain it's behaviour and how it may fix his problem. – Phiter Feb 16 '16 at 10:31
  • Covert d String to Array(char).d 1st loop searches thru d whole text... 2nd loop gets each char and finds it thru d original text.. for example i provide smtn like "google", 'g' will be searched thru "google".. and so on.. the len[i] holds the the char you searching with... if it is found int count is incremented... at the end of each search if count is > than 0.. and u test again if dat char doesnt exist in the vector(ch as here).. then u add the char(this is to avoid duplicatn).. and @ the course of that u print the count of each char...thats all. I 'am new here.. i just hope i tried.Thank u – Tobi Feb 16 '16 at 11:23
  • dats all i can say.. if you got any idea about the code or any simpler way u can explain it...put it up – Tobi Feb 16 '16 at 14:23