+ 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..

3rd Jan 2017, 9:32 AM
Anurag K
Anurag K - avatar
4 Antworten
+ 6
import java.util.Scanner int main(String[] args) { Scanner inpt = new Scanner(System.in); String word = "word"; if (inpt == word) { //code }}
3rd Jan 2017, 9:33 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 5
if(input.equals(word)){ // equal } else{ // not equal }
3rd Jan 2017, 9:36 AM
lowshuen
+ 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.
3rd Jan 2017, 11:05 AM
Marcin Barańczyk
Marcin Barańczyk - avatar
+ 1
if(inpt==word){ //code } not working......
3rd Jan 2017, 9:35 AM
Anurag K
Anurag K - avatar