arrays - how to sort my output alphabetically java -
i'm trying read 2 txt files 1 jumbled , 1 dictionary file match ones same letters. when words have 3 combinations doesn't come out alphabetically.
what fix this?
import java.io.*; import java.util.*; public class project6 { public static void main( string args[] ) throws exception { if (args.length<2) {system.out.println( "must pass name of 2 input files on cmd line. (dictionary.txt, jumbles.txt)"); system.exit(0); } long starttime = system.currenttimemillis(); // clicking start on stopwatch int maxdictwords = 180000; //has fixed length data structure... not familiar array list , faster. int maxjumblewords = 100; bufferedreader dictfile = new bufferedreader( new filereader(args[0]) ); bufferedreader jumblesfile = new bufferedreader( new filereader(args[1]) ); string[] dictionary = new string[maxdictwords]; // save dictionary txt array string[] jumbles = new string[maxjumblewords];// save jumblestxt array string [] sortdict = new string [maxdictwords]; //into sort dictionary string [] sortjumbles = new string [maxjumblewords]; // sort jumbles int dwordcnt=0, jwordcnt=0; arrays.sort(dictionary, 0, dwordcnt); // file reading while ( dictfile.ready() ) { string dictionaryword = dictfile.readline(); // grab whole line because whole line 1 word dictionary[dwordcnt] = dictionaryword; string scrambleword = tocanonical(dictionaryword); sortdict [dwordcnt] = scrambleword; dwordcnt++; } dictfile.close(); while ( jumblesfile.ready() ) // same above { string jumblesword = jumblesfile.readline(); jumbles[jwordcnt] = jumblesword; string scrambleword = tocanonical(jumblesword); sortjumbles [jwordcnt] = scrambleword; jwordcnt++; } jumblesfile.close(); string [] result = new string [maxjumblewords]; for(int k= 0; k < jwordcnt; k++){ { result[k]= jumbles[k] + " " ; (int j = 0; j < dwordcnt; j++) { if (sortjumbles[k].equals(sortdict[j])) { result[k]= result[k] + dictionary[j] + " "; } } } } arrays.sort(result,0, jwordcnt); for(int k = 0; k < jwordcnt; k++){ system.out.println(result[k]); } long endtime = system.currenttimemillis(); // clicking stop on stopwatch long ms = endtime-starttime; system.out.println("elapsed time in seconds: " + ms/1000.0 + "\n"); // 1 ms 1,000th of second } // end main // methods // sort letter canonical form private static string tocanonical(string word ) { char[]arr = word.tochararray(); arrays.sort(arr); string sorted = new string(arr); return sorted; } } // end project 6
if have choice, why not use in collection famous "lexicographical" sorting using sorted()
method?
for example (tried on ideone):
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* name of class has "main" if class public. */ class ideone { public static void main (string[] args) throws java.lang.exception { list<string> g = new arraylist<string>(); g.add("glove"); g.add("golve"); g.add("gvloe"); g.add("gevlo"); // before sorting (natural order) for(string s: g){ system.out.println(s); } system.out.println("\n----------"); collections.sort(g); // after sorting (natural order) for(string s: g){ system.out.println(s); } } system.out.println("----"); // convert array string[] p = g.toarray(new string[g.size()]); // check if array has correct sort order? (string s: p){ system.out.println(s); } }
does work you? n.b. code above redundant can sorting , convert array (which have correct sort order anyway). showed understand solution may prefer.
Comments
Post a Comment