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?

14th Dec 2019, 5:06 PM
НоĐșĐșĐž
НоĐșĐșĐž - avatar
3 Answers
+ 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.
14th Dec 2019, 5:18 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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.
14th Dec 2019, 5:58 PM
Avinesh
Avinesh - avatar
+ 2
Avinesh thanks for the correction. Didn't noticed about the return type.
15th Dec 2019, 4:04 PM
blACk sh4d0w
blACk sh4d0w - avatar