How do I sort an array of strings alphabetically in java?

satincorvo picture satincorvo · Dec 14, 2015 · Viewed 10.8k times · Source

I'm new to programming/coding and have been stuck on a project in school for a few days now. The goal is to take an array full of words (each position is a different word) and sort it alphabetically. I've tried doing some research on stack overflow already, but I'm having a bit of trouble following some of the examples I've found. The class and driver (I'm using a two part setup if you will) both compile fine, no problems there. The problem occurs when I try to use alphaSort from my driver. I receive a null pointer exception for the line marked below. I've had some trouble with these exceptions in the past, so I'm sure it's something small I'm overlooking. As stated however, I'm not yet fluent enough in the java syntax to catch a small error like that.

I figured I should just include the entire method in-case my error is something in the beginning, before the sorting part. What I have so far (i found this on Stack overflow):

public void alphaSort()
{
    String alphaList[] = new String[wordList.size()];
    int count=0;
    //puts wordList into alphaList for easier sorting
    while(count<wordList.size()-1)
    {
        alphaList[count]=wordList.get(count);
        count++;
    }
    int shortestStringIndex;
    //sort begins here
    for(int j=0; j<alphaList.length -1; j++)
    {
        shortestStringIndex = j;
        for(int i=j+1; i<alphaList.length; i++)
        {
            if(alphaList[i].trim().compareTo(alphaList[shortestStringIndex].trim())<0) //null pointer exception points here
            {
                shortestStringIndex = i;
            }
        }
        if(shortestStringIndex !=j)
        {
            String temp = alphaList[j];
            alphaList[j] = alphaList[shortestStringIndex];
            alphaList[shortestStringIndex]=temp;
        }
    }
    //prints out results
    count=0;
    while(count<alphaList.length)
    {
        System.out.println(alphaList[count]);
        alphaOut.print(alphaList[count]);
        count++;
    }
}

Any help would be greatly appreciated. Please be as thorough as possible in giving an answer (as i said, I'm a bit of a java newbie). Thanks :)

edit: to test for null values (which i assume are spots in my array list that are blank) i made the following method:

    public void isNull()
{
    int count=0;
    while(count<wordList.size()-1)
    {
        if((wordList.get(count)).equals(""))
        {
            System.out.println("null");
            break;
        }
        else
        {
            System.out.println("nothing yet");
        }
        count++;
    }
}

the while loop never broke early, my method ran to completion.

Answer

Sean Perkins picture Sean Perkins · Dec 14, 2015

You need to update the first while loop to match:

while(count < wordList.size()) {
            alphaList[count] = wordList.get(count);
            count++;
        }

You aren't copying over every index of the list to the array, which means that when it goes to check the last index, it cannot find a value (NullPointerException).

Edit:

Here's my full test class that works:

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    private ArrayList<String> wordList = new ArrayList<String>();

    public Test() {
        wordList.add("Test");
        wordList.add("Bee");
        wordList.add("Pig");
        wordList.add("Dog");
        alphaSort();
    }

    public void alphaSort() {
        String[] alphaList = new String[wordList.size()];
        int count = 0;
        while(count < wordList.size()) {
            alphaList[count] = wordList.get(count);
            count++;
        }
        int shortestStringIndex;
        for(int j = 0; j < alphaList.length - 1; j++) {
            shortestStringIndex = j;
            for(int i = j + 1; i < alphaList.length; i++) {
                if(alphaList[i].trim().compareTo(alphaList[shortestStringIndex].trim()) < 0) {
                    shortestStringIndex = i;
                }
            }
            if(shortestStringIndex != j) {
                String temp = alphaList[j];
                alphaList[j] = alphaList[shortestStringIndex];
                alphaList[shortestStringIndex]= temp;
            }
        }
        count = 0;
        while(count < alphaList.length) {
            System.out.println(alphaList[count++]);
        }
    }

}

Output:

Bee
Dog
Pig
Test