+ 2
How can compare between strings?
To verify one word i need to compare that word with inputed word....so i need string comparison....how can i perform that in java please help..
4 Réponses
+ 6
import java.util.Scanner
int main(String[] args) {
Scanner inpt = new Scanner(System.in);
String word = "word";
if (inpt == word) {
//code
}}
+ 5
if(input.equals(word)){
// equal
}
else{
// not equal
}
+ 3
Strings are objects so they should be compared with equals method. Just like @Iowshuen showed. I can just add that double equals checks if the objects are leading to the same object reference.
So to sum up
1. primitives should be compared with: ==
2. objects, like String should be compared with: equals method
3. double equals (==) used on object checks if objects lead to the same reference.
+ 1
if(inpt==word){
//code
}
not working......