1

For Strings..

How can i Detect if the string "name" has the text "Hello" a toast will come up saying yes but if it doesn't a toast will come up saying no? my code:

        String ft;
    if(ft.contains("Hello")){

        Toast.makeText(main.this ,"Yes", Toast.LENGTH_SHORT).show();
        }
        else{
        Toast.makeText(main.this ,"No", Toast.LENGTH_SHORT).show();
        }

I get a error at ft on if(ft.contains("Hello")) {

"The local variable ft may not have been initialized"

6
  • You have to initialize String empty. try String ft=""; Commented Oct 5, 2011 at 6:09
  • Thanks Kartik it worked, question will this save when the user closes the application and when they reopen it will be changed to whatever i put? Commented Oct 5, 2011 at 6:37
  • Let me resay that how can i change the string to be false after they click on my button? Commented Oct 5, 2011 at 6:40
  • question will this save when the user closes the application and when they reopen it will be changed to whatever i put?--- No it will be destroyed when you exit from application. Either you use Static String or SharedPreferences(if they are too important for you) Commented Oct 5, 2011 at 6:41
  • Yes, it's important i need it how could i do this? Commented Oct 5, 2011 at 6:42

4 Answers 4

2
String text;
if(name.equals("Hello")){
    text = "Yes";
} else {
    text = "No";
}

Toast toast = Toast.makeText(context, text, duration);
toast.show();

You can also use shorthand expression instead of if:

String text = name.equals("Hello") ? "Yes" : "No";
Sign up to request clarification or add additional context in comments.

6 Comments

Why not all in one line? Toast.makeText(context, (name.equals("Hello") ? "Yes" : "No"), duration).show();
String ft; if(ft.equals("Hello")){ ft = "Yes"; } else { ft = "No"; } i get a error under the ft at ft.equals "The local variable ft may not have been initialized"
This is gonna throw a compile time error... it is't right.. "text" should have atleast blank string value initially...
@Hailei: should be name.equals("Hello") not ft
My string name is FT not name
|
1

You declared String, but you didn't initialize. Initializing it is setting them equal to a value:

String ft;        // This is a declaration

String ft="";    // This is an initialization

Perhaps String ft = ""; is "declaration and initialization.

You get the error because you haven't initialized the variable, but you use them (e.g., ft.compare()) in if condition.

So try replacing String ft; with String ft="";

Comments

1
if(name.contains("Hello")){

Toast.makeText(activity.this ,"Yes", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(activity.this ,"No", Toast.LENGTH_SHORT).show();
}

Updated:

String ft = "";

    String ft= name.equals("Hello") ? "Yes" : "No";
     Toast.makeText(activity.this ,ft, Toast.LENGTH_SHORT).show();

2 Comments

The local variable ft may not have been initialized under if(ft.contains
@HaileiEdwards: change String ft; to String ft=""; or String ft=null; whatever suites best in your case.
0
if(name.equals("Hello")){
    Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
}else{
     Toast.makeText(this, "no", Toast.LENGTH_LONG).show();
}

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.