Home > Software design >  How to check if any strings in a list appears in a string, and then display anything that matches in
How to check if any strings in a list appears in a string, and then display anything that matches in

Time:01-24

I'm creating an OCR app that detects any text in live footage, and displays that text in a textbox. However, I want to filter this text to only display certain words, stored in a list.

This is the code I have currently that displays any text found

@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    final SparseArray<TextBlock> items = detections.getDetectedItems();
    if (items.size() != 0) {
        textView.post(new Runnable() {
            @Override
            public void run() {
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < items.size(); i  ) {
                    TextBlock item = items.valueAt(i);
                    stringBuilder.append(item.getValue());
                    stringBuilder.append("\n");
                }
                textView.setText(stringBuilder.toString());
            }
        });
    }
}

This is what I've tried using, but I'm getting no output

while (textView.getText().toString().trim().length() == 0) {
    if (list.contains(stringBuilder)) {
        for (int j = 0; j <= list.size(); j  ) {
            if (stringBuilder.toString().contains(list.get(j))) {
                textView.setText(list.get(j));
            }
        }
    }
}

Does anyone know how I can make the text view show only text that's in my list? Here is my list

List<String> list = new ArrayList<>();
words.add("abc");

I tried using the code below, because this on its own displays the live OCR feed, but it still didn't work above, so I believe the problem is in the checking process

textView.setText(stringBuilder.toString());
while (textView.getText().toString().trim().length() == 0) {
    if (list.contains(stringBuilder)) {
        for (int j = 0; j <= list.size(); j  ) {
            if (stringBuilder.toString().contains(list.get(j))) {
                textView.setText(stringBuilder.toString());
            }
        }
    }
}

CodePudding user response:

Your stringBuilder is a local variable, you can't refer it outside the method.

CodePudding user response:

I figured it out

StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < items.size(); i  ) {
                                    TextBlock item = items.valueAt(i);
                                    stringBuilder.append(item.getValue());
                                    stringBuilder.append("\n");
                                }

                                for (String item : list) {
                                    if (stringBuilder.toString().contains(item)) {
                                        matches = item;
                                    }
                                }

                                textView.setText(matches);
  • Related