equals() on incompatible types | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

equals() on incompatible types

So the problem is i get this error : equals() on incompatible types if (YE.equals("Exam")){ System.out.println(YE.nextLine()); HEO.Salad("S");} what should i do ? https://code.sololearn.com/cYA002DZEcVw

1st Oct 2018, 7:46 AM
PinkCode
PinkCode - avatar
19 Answers
+ 3
Pretty margaret What i said is that you cannot compare a Scanner object with a String (technically java allow you to do but .equals return always false). You probably want compare any line of string (received by Scanner throught .nextLine method). For do this, you have to compare the resulting strings. ex: String line= scanner.nextLine(); if(line.equals("angry")) ..... Beside this, like Ipang, me also dont understanded what have to do your code
1st Oct 2018, 6:07 PM
KrOW
KrOW - avatar
+ 5
@Margaret I'm sorry for delay, I was fast asleep back then, unfortunately still didn't understand how the idea relate with the code, as the code doesn't seem to be doing something as you described, I'm not native English speaking either, so my English is just average : ) What @KrOW said was right, need to get a String from Scanner object nextLine() method, (let's call that "haystack") and check if it contains some specific word (let's call this "needle"), if the "needle" was found in "haystack", then we know what word to use to reply as output, based on what "needle" was found in "haystack". I hope you understand my idea : ) Thanks @KrOW, for a hand when I was not around : )
2nd Oct 2018, 2:46 AM
Ipang
+ 4
@Margaret some examples please ... I still don't understand, @KrOW any ideas?
1st Oct 2018, 1:34 PM
Ipang
+ 4
Pretty margaret Dont worry... None was born learned 😉😉😉
1st Oct 2018, 6:53 PM
KrOW
KrOW - avatar
+ 3
You comparing a Scanner object (YE) with a String...
1st Oct 2018, 1:13 PM
KrOW
KrOW - avatar
+ 3
Pretty margaret Well, then you have to find a substring and not compare full line 😉
1st Oct 2018, 6:16 PM
KrOW
KrOW - avatar
+ 2
Besid @Ipang suggestion, i suppose that "YE" object is not a String
1st Oct 2018, 9:31 AM
KrOW
KrOW - avatar
+ 2
@Ipang ohhh i see , i just wanted to get a word and the see if the word is true then if the word is exactly the same as the one i gave it , do the other things ^^: ( sorry for my bad English , I'm actually new in this field(programming) so idk a lot about it ^^: ) and i wanted the output to String
1st Oct 2018, 1:32 PM
PinkCode
PinkCode - avatar
+ 2
@Ipang for example : you put "i'm hungry" then you get the output "you should eat" and then a list comes ... or... for example you put "it's 9 Am" then the program says "u should go to the work "
1st Oct 2018, 1:39 PM
PinkCode
PinkCode - avatar
+ 2
Ipang ???
1st Oct 2018, 6:05 PM
PinkCode
PinkCode - avatar
+ 2
KrOW ohhh i see thanks (*^▽^)/ i actually explained it for Ipang ... it's something like telling u what to do with the word u used ^^; for example u say "i'm hungry " then it returns "eat pizza" something like that
1st Oct 2018, 6:10 PM
PinkCode
PinkCode - avatar
+ 2
KrOW ohhh i see ! i'm learning java so i still don't know how to that ( basically i should study more ^^;) tnx again(〜^∇^)〜
1st Oct 2018, 6:52 PM
PinkCode
PinkCode - avatar
+ 2
Ipang Always ready for help if help is needed and if i can help 😉
2nd Oct 2018, 7:14 AM
KrOW
KrOW - avatar
+ 2
KrOW yup ( ^∇^)
2nd Oct 2018, 7:31 AM
PinkCode
PinkCode - avatar
+ 2
Ipang ohhhh thank you ლ(⌒▽⌒ლ) and that's ok every human need sleep〈( ^.^)ノ
2nd Oct 2018, 7:34 AM
PinkCode
PinkCode - avatar
+ 1
@Ipang done! thanks for helping me :D
1st Oct 2018, 1:08 PM
PinkCode
PinkCode - avatar
+ 1
@KrOW hmm...but i used string ... idk :(
1st Oct 2018, 1:09 PM
PinkCode
PinkCode - avatar
+ 1
@KrOW i can't ? what should i do ?
1st Oct 2018, 1:18 PM
PinkCode
PinkCode - avatar
+ 1
@Margaret maybe we can start with what output did you expect from the code, honestly I still don't understand what your intention with the code. For example, if input is x then expected output is y, something like that ...
1st Oct 2018, 1:21 PM
Ipang