Please excuse me for my formatting mistakes as I am very new here. I have a Java assignment, in which I have to find the lexicographic lowest word that is larger then the given word from a scanner without putting the string in lexicographic order. In this example the given word is "am". I have written the code below but I have the problems that I will explain at the end of this question.
public static void main (String[] args){
Scanner s = new Scanner("I am very happy with life");
String high = "";
String o = "am";
String curr = "";
while (s.hasNext()){
curr = s.next();
if (curr.compareTo(high) > 0)
high = curr;
}
Scanner ss = new Scanner("I am very happy with life");
while (ss.hasNext()){
curr = ss.next();
if (curr.compareTo(high) < 0 && curr.compareTo(o) > 0)
high = curr;
}
System.out.println(high);
}}
My problems are: I have to write a method that does the computation rather than having the procedure in main. Also I have to use the Scanner once and I can not initialize another scanner with the same value.
This code works ok, but I have the hardest times converting it into a single functional loop method.
PS. Sorry for stupid var names.