[Edited] it's always printing wrong weather i type yes or no or anything | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Edited] it's always printing wrong weather i type yes or no or anything

public class Program { public static void main(String[] args) { String answer; Scanner dec = new Scanner (System.in); answer = dec.next(); if (answer == "yes"){ System.out.println("right"); } else { System.out.println("wrong"); } } }

5th Mar 2017, 4:34 PM
周钰404
周钰404 - avatar
12 Answers
+ 7
You can't use just next with the scanner. You have to say dec.nextInt() or dec.nextLine(); in your case.
5th Mar 2017, 4:42 PM
J.G.
J.G. - avatar
+ 6
If it still isn't working then I suggest you post your code so that we can take a look at it, and maybe I can help you then, unless that is the entire code.
5th Mar 2017, 5:39 PM
J.G.
J.G. - avatar
+ 3
I don't have any answers right now but I will continue looking.
6th Mar 2017, 7:51 PM
J.G.
J.G. - avatar
+ 1
you need to use equals instead of ==
5th Mar 2017, 5:01 PM
Xin Xu
Xin Xu - avatar
0
J.G. I don't know how to reply you but the code is working with dec.next();
5th Mar 2017, 4:47 PM
周钰404
周钰404 - avatar
0
the only problem now I'm facing is weather I type yes o r no or anything it's printing wrong. it should print right when I type yes
5th Mar 2017, 4:48 PM
周钰404
周钰404 - avatar
0
J.G. If you can tell me how to reply you lol I don't know how to reply this is the entire code I just started learning java
6th Mar 2017, 2:39 AM
周钰404
周钰404 - avatar
0
Xin Xu not working
6th Mar 2017, 2:39 AM
周钰404
周钰404 - avatar
0
please post your code and your development environment…
6th Mar 2017, 2:59 AM
Xin Xu
Xin Xu - avatar
0
Xin Xu this is the code :( and used eclipse
6th Mar 2017, 12:25 PM
周钰404
周钰404 - avatar
0
Did you import Scaner? Import util.Scaner?
14th Mar 2017, 12:47 PM
Андрей Олейник
Андрей Олейник - avatar
0
string cannot be compared with ==
23rd Apr 2017, 5:12 PM
astitva