0
import java.io.*;
   import java.util.*;
   import javax.swing.JOptionPane;

   public class A15 {
      public static void main(String[] args) {
         if(args.length != 1) {
            JOptionPane.showMessageDialog(null, "Error: Enter one command line argument.");
         }
         else {
            try {
               String[] array = LeeJungBum15.readFileReturnWords(args[0]); // read file
               sortAndPrintArray(array, args[0]); // sort alphabetically and print the arrays
            } 
               catch(ArrayIndexOutOfBoundsException aobe) {
                  JOptionPane.showMessageDialog(null, "Error: Array is indexed out of bounds.");
               }
         }
      } // end of main
      
     /**
      * Reads file and returns word.
      * @param fileName is the String fileName
      */
      public static String[] readFileReturnWords(String fileName) throws ArrayIndexOutOfBoundsException {
         String[] array = new String[10000]; // declare String array
         Integer index = new Integer(0); // declare Integer index
        
         File file = new File(fileName); // read file
         Scanner scan = null;
         try {
            scan = new Scanner(file); // scan file
         }
            catch(FileNotFoundException fnfe) {
               JOptionPane.showMessageDialog(null, "Error: File is not found.");
            }
      	
         if(scan != null) {
            while(scan.hasNext()) {
               String word = scan.next(); // search words
               //System.out.println(word);
               array[index] = word; // store words in array
               ++index; // increment index
            }
         } 
         return array;
      }
      
     /**
      * Sorts and prints the array.
      * @param array is the String[] array
   	* @param fileName is the String fileName  
      */
      public static void sortAndPrintArray(String[] array, String fileName) throws NullPointerException {
         Sorting.display = false; // prevent the automatic output of the Sorting.java program
         Sorting.selectionSort(array); // sorts the String[] array
      	
         for(int i = 0; i < array.length; i++) {
            if(i == 0) {
               System.out.println("Alphabetical listing of words in file \"" + fileName + "\":"); // print the heading
            }
            else if(array[i] != null) {
               System.out.println("index = " + i + ", element = " + array[i]); // print indexes and elements
            }
            else {
               break; // terminate the program
            }
         }
      } // end of sortAndPrintArray
   } // end of class

This is supposed to take a filename from args[0], store each words as array and sort them alphabetically.

For example, if hello.txt contains these texts - B D E A C,
The output should be:
Index 0, Element A
Index 1, Element B
...
Index 4, Element E

However, I keep getting NullPointerException when Sorting.selectionSort(array) is activated.

If I comment that line out, it prints but it is not sorted.

Could anyone help me out? Thank you!

2
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by jaymozart
Featured Replies
  • You're confusing the array and the index. In the original code: [CODE] if(scan != null) { while(scan.hasNext()) { String word = scan.next(); array[index] = word; ++index; // this is also a count of the number of elements filled! } } return array;[/CODE] just replace the return statement with something like … Read More

0

Looks like the array is big, and you just populate the first "n" elements from the data file, so the remaining elements are un-initialised, and give an NPE when anyone tries to compare them for sorting.

0

I can't use ArrayList for this assignment and it seems like there is no way to set unknown number for Arrays after searching for hours...

0

I don't quite understand as I'm fairly new.

Do you mean something like this?
String[] newArray = array;

for(int i = 0; i < array.length; i++) {
newArray = array;
}

0

If i is the number of elements actually used - Yes.
But the syntax is String[] newArray = new String;
Or use Arrays.copyOf to do that for you.

Edited by JamesCherrill: Added correct syntax

1

You're confusing the array and the index. In the original code:

if(scan != null) {
while(scan.hasNext()) {
String word = scan.next();
array[index] = word;
++index; // this is also a count of the number of elements filled!
}
}
return array;

just replace the return statement with something like

return Arrays.copyOf(array, index); // returns a new array containing just the first "index" elements of "array"
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.