Question

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.

Was it helpful?

Solution

Something like this (using a TreeSet):

import java.util.Scanner;
import java.util.TreeSet;

public class LexicographicScanner {

    private final TreeSet<String> words = new TreeSet<String>();

    public LexicographicScanner( final Scanner scanner )
    {
        while ( scanner.hasNext() )
        {
            words.add( scanner.next() );
        }
        scanner.close();
    }

    public String nextWord( final String word )
    {
        return words.higher( word );
    }

    public static void main(String[] args) {
        final LexicographicScanner ls
            = new LexicographicScanner ( new Scanner("I am very happy with life") );

        System.out.println( ls.nextWord( "am" ) );
        System.out.println( ls.nextWord( "I" ) );
        System.out.println( ls.nextWord( "with" ) );
    }
}

Output

happy
am
null

Edit

Without a TreeSet:

public class LexicographicScanner {

    public static String nextWord( final Scanner scanner, final String word )
    {
        String higher = null, curr;
        while ( scanner.hasNext() )
        {
            curr = scanner.next();
            if ( curr.compareTo( word ) > 0 )
            {
                if ( higher == null || curr.compareTo( higher ) < 0 )
                    higher = curr;
            }
        }
        return higher;
    }

    public static void main(String[] args) {
        final Scanner s1 = new Scanner("I am very happy with life");
        final Scanner s2 = new Scanner("I am very happy with life");
        final Scanner s3 = new Scanner("I am very happy with life");
        System.out.println( nextWord( s1, "am" ) );
        System.out.println( nextWord( s2, "I" ) );
        System.out.println( nextWord( s3, "with" ) );
        s1.close();
        s2.close();
        s3.close();
    }
}

OTHER TIPS

First make some requirements:

  1. The word i am looking for is lexicographic higher than my input word
  2. The word i am looking for is the lexicographic lowest possible

So basically you have to walk over your input sentence word for word and check those two requirements. Here is some pseudo code, i hope this helps to understand your problem/this solution.

inputWord <= input
currentlyHighest <= null

for (word <= sentence) {
    is word higher than inputWord?
        no: discard word and analyze the next one
        yes: go on

    do i have a currently highest word?
        no: save the word in currentlyHighest and analyze the next word
        yes: go on

    is word lower than currentlyHighest?
        no: discard word and analyze the next one
        yes: we have found a better match: save word in currentlyHighest and analyze the next one
}

Hint:

String res = null;
Scanner s = new Scanner(str);
for each curr in s scanner {
    if (curr greater than given
            and (dont have res or curr is less than res)) {
        res = curr;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top