1

After finding a word in a sentence, I want to the user to be able to clear his entries and type again a sentence in the edittext.

This is what I have tried so far:

 final String[] words = {"cowboy", "animal"};
 final String[] meanings = { "meaning1", "meaning2" };
 Boolean check = false;

 private void initControls() {
    // TODO Auto-generated method stub
    text = (EditText) findViewById (R.id.editText1);

    view = (TextView) findViewById (R.id.textView1);

    clear = (Button) findViewById (R.id.button2);

    clear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            text.setText("");
            view.setText("");
        }
    });

    ok = (Button) findViewById (R.id.button1);
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            findAmbiguousWord();
        }   
    });
}

private void findAmbiguousWord(){
    String string = text.getText().toString();

    int index = 0;

    for (int i = 0; i < words.length; i++) {
        if (string.toLowerCase().contains(words[i].toLowerCase())) {
            check = true;
            index = i;
        } 
    }

    view.setText(check ? meanings[index] : "No ambiguous word/s found.");
}

When I tried to clear my entry and typed again new entry, the same result was displaying. what should be done to avoid displaying the previous result? Any help is much appreciated.

10
  • Do you mean that when you click on the clear button, the text in the TextView is not cleared? Commented Aug 25, 2013 at 19:59
  • You have to use addTextChangeListener for edittext. It will works. thanks. Commented Aug 25, 2013 at 20:00
  • @SharifurRahman In what way would a TextChangedListener help here? Commented Aug 25, 2013 at 20:02
  • Just one thing, I think you might want to have int index = -1; or have view.setText(check ? meanings[index] : "No ambiguous word/s found."); } inside the if condition. Also have an else statement there. I am saying this as it might be that you never enter the if condition when there is no substring of that type and then the result always is the word at meanings[0] as index is 0. Try that. Commented Aug 25, 2013 at 20:15
  • I think the EditText value is not getting reset !, an idea would be getting value of EditText onclick on button1; and passing the String to FindAmbigousWords; instead of working on a global Edittext. Commented Aug 25, 2013 at 20:24

2 Answers 2

1

Extract the checking functionality to a method like this:

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        checkAmbiguousWord();
    }   
});  

...

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    Integer ambiguousIndex = findAmbiguousWordIndex(textToCheck);
    view.setText(ambiguousIndex != null ? meanings[ambiguousIndex] : "No ambiguous word/s found.");
}

/**
 * @param text checked for ambguous words
 * @return the index of the ambiguous word in the {@code words} array or
 * null if no ambiguous word is found
 */
private Integer findAmbiguousWordIndex(String text) {
    final String lowerCasedText = text.toLowerCase();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            return i;
        }
    }
    return null;
}

This eliminates the dependence on a hidden internal state (the check variable). This programming style allows you to separate the view controller code and the business functionality and eventually write tests for the business functionality independently from the views.

UPDATE: to show multiple ambiguous words, use a list for the indexes

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    List<Integer> ambiguousIndexes = findAmbiguousWordIndexes(textToCheck);
    view.setText(!ambiguousIndexes.isEmpty() ? ambigousIndexesToMessage(ambiguousIndexes) : "No ambiguous word/s found.");
}

    public String ambigousIndexesToMessage(List<Integer> ambiguousIndexes) {
        // create the text using the indexes
        // this is an example implementation
        return ambiguousIndexes.toString(); // creates a list of "1","2",...
    }

/**
 * @param text checked for ambguous words
 * @return the list of indexes of the the ambiguous words in the {@code words} array          
 */
private List<Integer> findAmbiguousWordIndexes(String text) {
    final String lowerCasedText = text.toLowerCase();
    final List<Integer> ambiguousWordIndexList = new ArrayList<Integer>();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            ambiguousWordIndexList.add(i);
        }
    }
    return ambiguousWordIndexList;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for explaining! I can better understand it. Anyways, can it allow spaces?
Can you elaborate a little more? The String.contains() method looks for a substring in a string. It doesn't consider spaces or other delimiters, just chcecks if the input substring is present in the string you called the contains() on.
Hi I got question again, what if I type more than one word in the array, it only displays the first word it fetches. How can I display the other words?
Added example. Basically you need to use a list for the indexes. Note that the message constructor method implementation is a really basic one. I trust you can write whatever you want :)
Okay but in your added example, when I tried to input two words, it gave me no ambiguous words
|
0

You need to set the check to false at start of method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.