0

I started with JAVA about 2 months ago by myself so I'm sorry if I write something stupid : pp

I think all my questions was answered here but this one I didn't find exactly what I want. My question is:

I have an app with a single EditText and a Button, the users enter a text and the button will analyze it. I also have 2 boxes, one with apple and orange, another with lemon and potato.

If users type: "I want apple", the program will say: "It is inside Box 1". But the user can type whatever he want but the food's name will never change, it will be apple, orange, potato or lemon. So how can I say to the program: If (MyEditText contains "apple"), show box 1, else if (MyEditText contains "lemon") show box 2?

I'm doing this app because I want to learn more and more about Android Development. Hope I was clear.

Sorry about my English

mari
  • 864
  • 3
  • 12
  • 32
Murillo Ferreira
  • 1,423
  • 1
  • 16
  • 31

4 Answers4

2

I am not android developer but based on this answer you can try something like

if(myEditText.getText().toString().contains("apple")){//...
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • It works fine when I'm only using one condition in If else, i need to create a string to use it in various conditions as R9j did. Thanks for your help !! – Murillo Ferreira Jul 04 '13 at 04:36
  • @MurilloHenrique I assumed that since you are learning java for 2 months you must knew about `if(condition){ doIfConditionIstrue; }else{ doIfConditionIsFalse; }` so didn't include `else` part in my answer assuming that it is obvious. My mistake :) – Pshemo Jul 04 '13 at 05:52
  • @ Pshemo Sorry, I wasn't clear to you. What you did works very well in if else, but not with Logical operators, the IDE gives an error if do not create a String before, so what you did doesn't work if I do something like this: if (myEditText.getText().toString().contains("apple") && myEditText.getText.toString.contains("orange")){... – Murillo Ferreira Jul 04 '13 at 17:30
  • @MurilloHenrique sorry, but shouldn't there be `myEditText.getText().toString().contains("orange")` instead of `myEditText.getText.toString.contains("orange")` - I added few () where we invoke methods. Also I didn't mean `if (myEditTex...contains("apple") && myEditTex...contains("orange"))` since you already mentioned `If (MyEditText contains "apple"){ show box 1} else if (MyEditText contains "lemon") {show box 2}`. – Pshemo Jul 04 '13 at 19:49
  • No problem, good to know that Java works as it should also on Android :) – Pshemo Jul 05 '13 at 00:44
  • @Phsemo I forgot those "()" when I was writing here, anyway, I deleted my code and I rewrote it and it works perfectly, I don't what I did wrong, I tried it many times but it works when I rewrote my code, maybe I made something stupid : p. I appreciate your help and sorry about all of this. My intention with all of this is, if the user write "apple", show box one, but if he write I don't want apple, I need the app find "don't" AND "apple",in this case, I'll use the logical operators. I know it doesn't have sense, but I want to learn and understand how everything works. Thanks !! – Murillo Ferreira Jul 05 '13 at 00:49
  • @MurilloHenrique If you want to use logical operators you can try to do this like this (from more detail if to less detail) `if ( contains Apple && contains don't){action for don't and apple}else{if ( contains Apple){only apples without don't action}}`. But you can optimize it a little and decrease number of `contains` by writing it as `if ( contains apples){if (contains don't){action for don't and apples} else {action for only apples} }` – Pshemo Jul 05 '13 at 01:06
  • the second way is away much better and less confuse, I can easily add more conditions or actions inside the Apple condition, the computer will automatically permute all the possibilities (If I decide to add more words to be checked within the Apple) I really appreciate your help !! Thanks. – Murillo Ferreira Jul 05 '13 at 01:42
0

You can use indexOf method to check if a specific string exists in the text. Alternatively You can use contains method. If you would prefer to ignore the case, convert it all to upper case and compare like:

EditText myEditText = (EditText) findViewById(R.id.edittext);
String myEditTextValue = myEditText.getText().toString();
String valueInUpperCase=myEditTextValue.toUpperCase() 

if(valueInUpperCase.contains("APPLE")) {
    // show box 1
} else if(valueInUpperCase.contains("LEMON")) {
    // show box 2
}

See Java Docs for String

Hope this helps you.

Naresh
  • 3,174
  • 1
  • 17
  • 22
neo108
  • 5,156
  • 3
  • 27
  • 41
  • Let me see if I'm right, if I use the UpperCase, Apple would be equal to ApPlE and so on ? This will help me a lot ! Probably someone used the indexOf somewhere, I'll search for it, this is exactly what I had on my mind, compare an especific String in the EditText. Thx for your help ! – Murillo Ferreira Jul 04 '13 at 04:41
0

I am assuming that you wan't the input to to be processed when the user clicks the button. You can do something like this

   btnOK.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                String userInput = MyEditText.getText().toString();
                if(userInput.contains("apple")){
                    //Show it is inside box 1
                }else if(userInput.contains("orange")){
                     //Show it is inside box 2
                }
            }
        });

Here btnOK refers to your button whatever you called it. As far as displaying the information, I am not sure about what you mean by "I have 2 boxes." If by boxes you mean textView which already has the text ("it is in box 1") ("it is in box 2") in it. You can simply change the visibility attribute. Depending on condition

textView1.setVisibility(View.VISIBLE); //textview containing ("it is in box 1")
textView2.setVisibility(View.INVISIBLE);   //textView containing ("it is in box 2")

Or you can just display the result by populating a textView. textView.setText("Your text");

Hope this helps.

Naveed
  • 2,942
  • 2
  • 25
  • 58
  • When I said "Boxes" I mean Real world boxes that I have 2 types of food inside each one, the program would allow the user to find where each food is stocked. Sorry about it : ) – Murillo Ferreira Jul 04 '13 at 04:23
0

Try this

String enteredText = editText.getText().toString();
if(enteredText.contains("apple")){
    ......
}
else if(enteredText.contains("orange")){
    ......
}

Hope this helps.

R9J
  • 6,605
  • 4
  • 19
  • 25