0
How can I equate a word from Scanner to a String word in java? There is more in the description
I tried to equate, but there is nothing outputted in the console or occures a mistace "incompatibility operated types Scanner and String". Scanner c = new Scanner; If(c == "a word") { System.out.println("something outputs"); } How can I write this another way? And is it possible?
3 Antworten
+ 2
Scanner c = new Scanner(System.in);
// String input
String str = c.nextLine();
// For string comparison
if(str.equals("a word") == 0) {
// True case statement(s)
}
Note:
== is an operator while .equals() is a method.
== operator is used to compare references(addresses) of two strings, on the other hand .equals() method is used to compare the content of two strings.
+ 4
nAutAxH AhmAd Just a small correction.
The equals method will return a boolean and 0 is an integer value so the comparison is not happening.
It will return a type mismatch error.
+ 2
Avinesh thanks for the correction. Didn't noticed about the return type.