使用.contains方法匹配多个单词

huangapple 未分类评论47阅读模式
英文:

Use .contains with multiple words

问题

Good day, How can I get to use contain with multiple words?

String word = "word"

answer1.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (edittext.getText().toString().contains(word)){
            Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();

That is the code if only a single word, in multiple words I use this:

final String[] stringToCheck = new String[]{"word", "life"};
for (final String s : stringToCheck)
    answer1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (answer1.getText().toString().contains(s)){
                Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();

if I remove the line for there is an error.

but that code doesn't fit in my case, because I am using this layout many times.
So is there an alternative way or a simple way to use the contain method with multiple words without using a string array?
Or can I insert a string array into another string array? (because I think this will solve my problem)

英文:

Good day, How can I get to use contain with multiple words?

 String word = "word"

 answer1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (edittext.getText().toString().contains(word)){
                        Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();

That is the code if only a single word, in multiple words I use this:

     final String[] stringToCheck = new String[]{"word", "life"};
    for (final String s : stringToCheck)
  answer1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (answer1.getText().toString().contains(s)){
                        Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();

if i remove the line for there is an error.

but that code doesn't fit in my case, because i am using this layout many times.
So is there an alternative way or simple way to use contain method in multiple word without using stringarray?
Or Can i insert stringarray on another string array? (because i think this will solve my problem)

答案1

得分: 0

尝试使用

public boolean onKey(View v, int keyCode, KeyEvent event) {

    String answer = answer1.getText().toString();

    for (String s : stringToCheck) {
        if (answer.contains(s)) {
            Toast.makeText(QuestionActivity.this, "正确!", Toast.LENGTH_SHORT).show();
            break;
        }
    }
}
英文:

Try using

             public boolean onKey(View v, int keyCode, KeyEvent event) {

                String answer = answer1.getText().toString();

                for (String s : stringToCheck) {
                    if (answer.contains(s)) {
                        Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }

答案2

得分: 0

当添加那个for循环时,你正在多次覆盖OnKeyListener。你可以这样做:

final String[] stringToCheck = new String[]{"word", "life"};
final Stream<String> wordsStream = Stream.of(stringToCheck);

answer1.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (wordsStream.anyMatch(s -> answer1.getText().toString().contains(s))) {
            Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();

这将对流中的所有单词与答案进行检查,只要其中任何一个匹配,就会触发Toast。

如果你需要让它们都同时匹配,你可以将Stream::anyMatch替换为Stream::allMatch。

英文:

What you are doing when adding that for loop is overwrite the OnKeyListener multiple times.
What you could do is the following:

final String[] stringToCheck = new String[]{&quot;word&quot;, &quot;life&quot;};
final Stream&lt;String&gt; wordsStream = Stream.of(stringToCheck);

  answer1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
           if (wordsStream.anyMatch(s -&gt; answer1.getText().toString().contains(s))) {
                        Toast.makeText(QuestionActivity.this, &quot;Correct!&quot;, Toast.LENGTH_SHORT).show();

This would check all the words in the stream against the answer and as soon as any of them match it will trigger the Toast.

Should you need to make them all match at once then you can replace Stream::anyMatch with Stream::allMatch

huangapple
  • 本文由 发表于 2020年5月4日 03:20:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定