java - Finding the alphabetically last word in a file read using scanner -
i working on assignment have find alphabetically last word in file read using scanner. of examples looked suggested using arrays or arraylists cannot use arrays or arraylists part collections out of question. have been trying use compareto() compare strings in lexicographical order. far have like:
while (read.hasnext()) { string s = read.next().tolowercase(); }
now question how implement compare in instance? tried doing
if (string1.compareto(string2) < 0) { lastword = string1 }
but having trouble creating 2 different strings compare with. ideas or hints helpful.
perhaps like:
string lastword = ""; while (read.hasnext()) { string s = read.next().tolowercase(); if (lastword.compareto(s) < 0) lastword = s; }
or taking @ulvon's suggestion consideration can preserve string unchanged.
string lastword = ""; while (read.hasnext()) { string s = read.next(); if (lastword.comparetoignorecase(s) < 0) lastword = s; }
n.b. haven't compiled either solution syntax may not quite right.
Comments
Post a Comment