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”
You declared
String, but you didn’t initialize. Initializing it is setting them equal to a value: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;withString ft="";