-1

単語のスタックがあり、「w」文字を含む最も長い単語を取得する必要があります。私はこうしてきました

public static boolean longestWWord(String s)
{
    boolean hasw=false;
    for(int i = 0; i < s.length(); i++)
    {
        if(s.charAt(i)=='w')
        {
            hasw = true;
        }
    }
    return hasw;
}
    System.out.println("Word with maximum 'w' chareckters : ");
    int counter =0;
    String word="";
    for ( String ss : arr) {
        if(longestWWord(ss)){
            if(ss.length()>counter)
            {
                counter = ss.length();
                word = ss;
            }
        }
    }
    System.out.println(word);

しかし今、単語に「w」が含まれているかどうか、および正規表現を使用してその単語の長さを見つけるタスクがあります。私を助けてください

4

2 に答える 2

1
public class MatcherTest {

    public static void main(String[] args) {

        String word = "wata what word down a adf asdfasdf";

        Pattern p = Pattern.compile("\\w*[wW]+\\w*");
        Matcher m = p.matcher(word);

        while (m.find()) {
            System.out.println(m.group());
        }
    }
}

出力します:

wata
what
word
down

ここでドキュメントを見つけることができますhttp://docs.oracle.com/javase/tutorial/essential/regex/index.html

于 2013-10-18T04:40:12.140 に答える