1

I have a method that works fine and gives desired result. Here is it's code:

public class arraycomparison {
public static void main(String args[]) {
 try {
 Scanner sc1 = new Scanner(new FileInputStream("/Users/esna786/File1.txt"));
 Scanner sc2 = new Scanner(new FileInputStream("/Users/esna786/File2.txt"));
 List<String> File1Content = new ArrayList<String>();
 List<String> File2Content = new ArrayList<String>();
 List<String> commonWords = new ArrayList<String>();
 //Getting the contents of File1
 while (sc1.hasNext()) {
 File1Content.add(sc1.next());
 }
 String arr1[] = File1Content.toArray(new String[File1Content.size()]);
 //Getting the contents of File1
 while (sc2.hasNext()) {
 File2Content.add(sc2.next());
 }
 String arr2[] = File2Content.toArray(new String[File2Content.size()]);
 int count = 0;
 for (int i = 0; i < arr1.length; i++) {
 for (int j = 0; j < arr2.length; j++) {
 if (arr1[i].equals(arr2[j])) {
 commonWords.add(arr1[i]);
 count++;
 }
 }
 }
 String[] comWords = commonWords.toArray(new String[commonWords.size()]);
 System.out.println("Total Number of Common Words are: "+comWords.length);
 for (int i = 0; i < comWords.length; i++) {
 System.out.println(i + 1 + ": " + comWords[i]);
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
}

}

it's out put is,

Total Number of Common Words are: 7
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
BUILD SUCCESSFUL (total time: 0 seconds)

Using the same files when I run the program code below almost same as above:

//Counting the number of matched words
public int getCommonWords(File File1, File File2) throws IOException {
 try {
 Scanner sc1 = new Scanner(new FileInputStream(File1));
 Scanner sc2 = new Scanner(new FileInputStream(File1));
 List<String> File1Content = new ArrayList<String>();
 List<String> File2Content = new ArrayList<String>();
 List<String> commonWords = new ArrayList<String>();
 //Getting the contents of File1
 while (sc1.hasNext()) {
 File1Content.add(sc1.next());
 }
 String arr1[] = File1Content.toArray(new String[File1Content.size()]);
 //Getting the contents of File1
 while (sc2.hasNext()) {
 File2Content.add(sc2.next());
 }
 String arr2[] = File2Content.toArray(new String[File2Content.size()]);
 int count = 0;
 for (int i = 0; i < arr1.length; i++) {
 for (int j = 0; j < arr2.length; j++) {
 if (arr1[i].equals(arr2[j])) {
 commonWords.add(arr1[i]);
 count++;
 }
 }
 }
 String[] comWords = commonWords.toArray(new String[commonWords.size()]);
 System.out.println("Total Number of Common Words are: " + comWords.length);
 for (int i = 0; i < comWords.length; i++) {
 System.out.println(i + 1 + ": " + comWords[i]);
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 return 0;
}

it displays following in the output area:

run:
Total words of File1.txt are: 11
Total Number of Common Words are: 11
1: apple
2: ball
3: cat
4: duck
5: elephant
6: fan
7: goat
8: a
9: b
10: c
11: d

it is even displaying the unmatched words as well, why is it so? Please help.

asked Dec 28, 2014 at 5:16
4
  • 5
    Initialise commonWords as the stacktrace indicates... Commented Dec 28, 2014 at 5:18
  • I initialized it to null, it should later be occupied with other values in loops. Commented Dec 28, 2014 at 5:19
  • 3
    Heck no. You initialize it to null, to nothing. You can't assign items to a null array. You're confusing the array with the array items. Commented Dec 28, 2014 at 5:20
  • null is not an array. null[i] produces an NPE. Commented Dec 28, 2014 at 5:20

2 Answers 2

3

You are getting the null pointer exception for an obvious reason, the variable commonWord is not pointing to an array (you didn't allocate memory for storing the array strings).

String commonWords[]=null;

You have 2 solutions:

  • Use fixed memory allocation:

    String commonWords[]= new String[Math.min(arr1.length, arr2.length)];
    int commonCount = 0;
    ....
    commonWords[commonCount++] = arr2[i]
    
  • Use dynamic memory allocation:

    ArrayList<String> commonWords = new ArrayList<>();
    ...
    commonWords.add(arr2[i]);
    
answered Dec 28, 2014 at 5:27
0
2

If you want to preserve your logic and data structures, then use something like:

String commonWords[] = new String [Math.min(arr1.length, arr2.length)];

It's far from perfect, but will allow you to assign values to your array.

Then to print:

for(String str : commonWords) {
 if (str != null) {
 System.out.println(str);
 }
} 
answered Dec 28, 2014 at 5:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.