java - Read text file to arrayList and sort -
this snippet of txt file "q.txt".
12.54778255173505 : ^finishedline 15.416218875438748 : ^finishedline 16.245508427720914 : ^finishedline 9.595696051997852 : &^finishedline 11.971100145959943 : ! '^finishedline 11.678678199807727 : " $^finishedline 14.905855346233682 : # %^finishedline 15.98343143372184 : $ "^finishedline 16.053542916378102 : % #^finishedline
i need sort text file "q.txt" contains double , string. has been separated using " : " , @ end of each phrase there ("^finishedline"). when run this, coming "numberformatexception: empty string" error.
public class sorting { public static void sort() throws ioexception { scanner s = new scanner(new file("q.txt")); arraylist<qpair> set = new arraylist<>(); string line = ""; while (s.hasnext()) { string[] parts = line.split(" : "); set.add(new qpair(double.parsedouble(parts[0]), parts[1])); s.usedelimiter("^finishedline"); } s.close(); system.out.println(set); } private static class qpair{ private double d; private string s; public qpair(double d, string s){ this.d = d; this.s = s; } public double getdouble(){ return d; } public string getstring(){ return s; } } private static class qpaircompare implements comparator<qpair>{ public int compare(qpair x, qpair y){ return (int) (x.getdouble() - y.getdouble()); } } }
as far can see: setting line
empty string. enter while
loop without changing line
. split empty string @ :
. admit had try out sure: yields array of 1 element, empty string. try parse empty string array double.
and tell numberformatexception: empty string
. sounds agreement me.
i hope can figure out code line missing , should insert it?
Comments
Post a Comment