Having trouble with my if statements using user input for the variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Having trouble with my if statements using user input for the variables

I keep getting the error result printed no matter what I type in as the user input. Was hoping to get some help! Thanks in advance! https://code.sololearn.com/cayPkXmF29Bu/?ref=app

23rd Jul 2019, 3:00 PM
Gabe Bacus
Gabe Bacus - avatar
6 Answers
+ 2
Because of == If you compare int, long, char or other primitive data types then == is okay. But for String you need equals(). If(choice.equals("attack"){ //print attack }else if(choice.equals("defend"){ //print defend }else{ //print error }
23rd Jul 2019, 5:34 PM
Denise Roßberg
Denise Roßberg - avatar
23rd Jul 2019, 5:35 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
thank so much !!
23rd Jul 2019, 5:40 PM
Gabe Bacus
Gabe Bacus - avatar
0
What did you type as user input? I typed 1 and 2, and it's doing alright for both run
23rd Jul 2019, 3:20 PM
Agent_I
Agent_I - avatar
0
Oh I had just changed it to use int as the values. I orginally was using a string variable and getting user input through “option.next();”
23rd Jul 2019, 3:31 PM
Gabe Bacus
Gabe Bacus - avatar
0
sorry about that ive changed it back to what i had originally
23rd Jul 2019, 3:33 PM
Gabe Bacus
Gabe Bacus - avatar