0

I'm trying to sort a list of strings in array into alphabetical order without using the sort method.

public static String[] sortedAdjectives(String[] original)
{
    String[] sortedArray;
    int aValue = 65;
    String word = "";
    sortedArray = new String[25];
    for(int i = 0; i <25; i++)
    {
        original[i]=word;
        char c = word.charAt(0);
        sortedArray[c-aValue]=word;

    }
    return sortedArray;

Is my method, and

public static void main(String[] args) throws FileNotFoundException {
        Scanner names = new Scanner(new File("names.txt"));
        Scanner adjectives = new Scanner(new File("adjectives.txt"));
        String[] adjectiveArray;
        adjectiveArray = new String[25];
        int counter = 0;
        while (counter<25)
        {
            String in = adjectives.next();
            fixCapitalization(in); //method that fixes capitalization
            adjectiveArray[counter]=in;
            counter++;
        }
        sortedAdjectives(adjectiveArray);

Is where I put the items from the file into an array. I'm getting

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at Hmwk.sortedAdjectives(Hmwk.java:56)
    at Hmwk.main(Hmwk.java:24)

When I try to run my program and I can't figure out where I'm going wrong. If you could point me in the right direction i'd be much appreciative. Thanks for your time.

2
  • In your sort method, word is the empty string. You're overwriting all values of your input array with this, and trying to access a character that doesn't exist. Commented Mar 10, 2014 at 19:38
  • You are making many assumptions about your input strings (several wrong , like the assumption that there are only 25 adjectives. You strategy should be to implement a method that sorts an array of Strings and call that. Commented Mar 10, 2014 at 19:49

2 Answers 2

3

You have word initialized as an empty string:

String word = "";

Then you are calling charAt(0) on an empty string. Can't do that. Your string needs to be at least longer than 1 character in order to call that method.

Sign up to request clarification or add additional context in comments.

3 Comments

Apart of this, it would be better to add that since Strings are immutable, calling method fixCapitalization should return a String and its value should replace the value of in, unless this method modifies the String interns (which would be really odd).
But wouldn't original[i]=word; make word equal to the item at the index of 0?
No, you have to use word = original[i] instead. With original[i]=word you are assigning the value of word to original[i].
0

You made a little mistake in the for loop. It should probably be word = original[i]; which you did it inversely and makes the word never take the original parameter as reference.

Also a few things to improve here: using arraylist would have better extensibility and avoid erasing repetitive letters.

Comments

Your Answer

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.