0

Java で文字列とリストを使用する方法について質問があります。たとえば、文字列を入力できるようにしたい

「ああ」

スキャナ クラスを使用すると、プログラムは 3 つの a を含む最短の単語を返す必要があります。たとえば、入力でチェックされる何千もの単語で満たされたテキスト ファイルがあり、その中に a が 3 つある場合、それは候補ですが、現在はその 1 つだけを返すのが最短です。文字の入力が、単語で満たされたテキスト ファイルのすべての単語に含まれているかどうかを比較して確認するには、どうすればよいでしょうか。

4

3 に答える 3

2

JavaDocs をjava.lang.String参照することから始めます

特に、 をご覧くださいString#contains。パラメーターの要件のために、これを見逃したことをお許しください。

例:

String text = //...
if (text.contains("aaa")) {...}
于 2013-10-18T02:36:43.670 に答える
0

これを試して、

          while ((input = br.readLine()) != null)
            {
                if(input.contains(find)) // first find the the value contains in the whole line. 
                {
                   String[] splittedValues = input.split(" "); // if the line contains the given word split it all to extract the exact word.
                   for(String values : splittedValues)
                   {
                       if(values.contains(find))
                       {
                           System.out.println("all words : "+values);
                       }
                   }
                }
            }
于 2013-10-18T02:37:00.853 に答える
0

最も簡単な方法は、String.contains()長さをチェックするループを使用することです。

String search = "aaa"; // read user input
String fileAsString; // read in file
String shortest = null;
for (String word : fileAsString.split("\\s*")) {
    if (word.contains(search) && (shortest == null || word.length() < shortest.length())) {
        shortest = word;
    }
}
// shortest is either the target or null if no matches found.
于 2013-10-18T02:46:03.050 に答える