I am writing a code in java. I want to ask the user for input for ayes/no questi
ID: 3616943 • Letter: I
Question
I am writing a code in java. I want to ask the user for input for ayes/no question and use the yes or no answer like this:int y = (m.indexOf('/'));
if (y == -1) {
System.out.println("invalid");
System.out.println("tryagain?");
String w = scan.nextLine();
if (w == "yes") {
System.out.println("okthen");
}
else if (w == "no") {
System.out.println("exiting");
every time I run this code, it just repeats even when I type inno.
So my question is:
How do I ask for a yes/no user input and how would I know if thatuser typed in exactly yes or no.
Explanation / Answer
please rate - thanks to compare strings in java you must use one of the many comparemethods int y = (m.indexOf('/')); if (y == -1) { System.out.println("invalid"); System.out.println("tryagain?"); String w = scan.nextLine(); if(w.compareTo("yes")==0) { System.out.println("okthen"); } else if(w.compareTo("no") == 0) { System.out.println("exiting");
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.