Java - 按字典顺序查找下一个单词

时间:2014-02-12 10:07:03

标签: java java.util.scanner lexicographic

请原谅我的格式错误,因为我在这里很新。 我有一个Java分配,我必须找到字典的最低单词,它比扫描器中的给定单词大,而不是按字典顺序排列字符串。在这个例子中,给定的单词是“am”。我已经编写了下面的代码,但我有问题,我将在这个问题的最后解释。

    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);
}}

我的问题是: 我必须编写一个执行计算的方法,而不是在main中使用该过程。此外,我必须使用扫描仪一次,我无法初始化具有相同值的另一台扫描仪。

这段代码运行正常,但我最难将其转换为单一功能循环方法。

PS。抱歉愚蠢的var名称。

3 个答案:

答案 0 :(得分:2)

像这样(使用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" ) );
    }
}

<强>输出

happy
am
null

修改

没有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();
    }
}

答案 1 :(得分:2)

首先提出一些要求:

  1. 我要找的字是词典,高于我的输入词
  2. 我正在寻找的词是最低可能的词典
  3. 所以基本上你必须逐字逐句地查看输入句子并检查这两个要求。这是一些伪代码,我希望这有助于理解您的问题/此解决方案。

    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
    }
    

答案 2 :(得分:1)

提示:

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;
    }
}