How do i write an if
statement in Java that displays Goodbye!
if the variable word
contains a letter d
?
Thanks to everyone.
How do i write an if
statement in Java that displays Goodbye!
if the variable word
contains a letter d
?
Thanks to everyone.
Use:
if(word.indexOf("d") >= 0) {
System.out.println("Goodbye!");
}
Look up the Java API docs to see what is available in the String class. There are several options including the indexOf() method that returns -1 if the given character is not in the String and an index of the character if it is found in the String.
int ans = mystring.indexOf(mychar);
You can then use an if statement to check the ans variable.
if (word.contains("d")) System.out.println("Goodbye!");
Well, that was in Java!!
if(word.compareTo("d") == 0)
System.out.println("Goodbye!");
or
if(word.equals("d"))
System.out.println("Goodbye!");
That's assuming of course that word is a String and not a char.