java - Sorting Strings in an Array by Char -
i'm trying sort list of strings in array alphabetical order without using sort method.
public static string[] sortedadjectives(string[] original) { string[] sortedarray; int avalue = 65; string word = ""; sortedarray = new string[25]; for(int = 0; <25; i++) { original[i]=word; char c = word.charat(0); sortedarray[c-avalue]=word; } return sortedarray; is 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 fixes capitalization adjectivearray[counter]=in; counter++; } sortedadjectives(adjectivearray); is put items file array. i'm getting
exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 0 @ java.lang.string.charat(unknown source) @ hmwk.sortedadjectives(hmwk.java:56) @ hmwk.main(hmwk.java:24) when try run program , can't figure out i'm going wrong. if point me in right direction i'd appreciative. time.
you have word initialized empty string:
string word = ""; then calling charat(0) on empty string. can't that. string needs @ least longer 1 character in order call method.
Comments
Post a Comment