I need some help debugging this Java code please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

I need some help debugging this Java code please.

I thought it was working perfectly, but I just realized that is not the case. Bug: If player B wins the match, the declaration of the winner is not printed. for instance, if you input 1 as player A and 7 as Player B (Cloud vs Defaultius Maximus) Cloud will lose the fight, but it will not declare Defaultius as the winner, the code execution just stops. https://code.sololearn.com/c22FNtmaFPkQ/?ref=app

13th Oct 2017, 4:29 AM
Mark Paramonte
Mark Paramonte - avatar
25 Answers
+ 4
Heres the code :-(its not public, so u dont need to worry xD) https://code.sololearn.com/cZD8s3xiD1MP/?ref=app
13th Oct 2017, 4:57 AM
Meharban Singh
Meharban Singh - avatar
+ 5
If user enters any integer except for 1-6, the pick will default to Defaultius Maximus. I will work on adding a way to handle a NaN input. Thanks for the tip!
13th Oct 2017, 5:14 AM
Mark Paramonte
Mark Paramonte - avatar
+ 5
Actually, I already took care of the type validation issue. @line 152 in the repaired code you provided.
13th Oct 2017, 5:17 AM
Mark Paramonte
Mark Paramonte - avatar
+ 5
Yes!! I didn't notice.
13th Oct 2017, 5:19 AM
Meharban Singh
Meharban Singh - avatar
+ 5
Your if, if blocks(last 2 ones) should be out of the while loop!!. If they are inside, the break statement indirectly affects them as well!!
13th Oct 2017, 6:45 AM
Meharban Singh
Meharban Singh - avatar
+ 5
Its OK :-)
13th Oct 2017, 6:48 AM
Meharban Singh
Meharban Singh - avatar
+ 4
The problem seems to be within this block of code, which is nested inside of a Do..While loop. The second If statement never executes. if (bHp <= 0){ //If second fighter health drops to <= zero. System.out.println(a.choiceToString() + " has defeated " + b.choiceToString() + " in battle." + "\n" + a.choiceToString() + " is the WINNER!"); continue; } if (aHp <= 0){ //If first fighter health drops to <= zero. System.out.println(b.choiceToString() + " has defeated " + a.choiceToString() + " in battle." + "\n" + b.choiceToString() + " is the WINNER!"); continue; } I tried turning it into an If...Else statement, but I get the same result, the second statement does not execute.
13th Oct 2017, 4:37 AM
Mark Paramonte
Mark Paramonte - avatar
+ 4
Move if blocks at the the last, after line 216
13th Oct 2017, 4:49 AM
Meharban Singh
Meharban Singh - avatar
+ 4
I re-repaired it, chk out from above link. Also, you can replace while with do while, it doesnt matter
13th Oct 2017, 5:08 AM
Meharban Singh
Meharban Singh - avatar
+ 4
:-) My pleasure Anytime :-)
13th Oct 2017, 5:14 AM
Meharban Singh
Meharban Singh - avatar
+ 4
Anytime :-)
13th Oct 2017, 5:15 AM
Meharban Singh
Meharban Singh - avatar
+ 4
So, I updated my code and now I'm getting an error about my scanner object...I didn't change anything regarding my scanner -_- https://code.sololearn.com/c2BxG805Ub2v/?ref=app
13th Oct 2017, 6:12 AM
Mark Paramonte
Mark Paramonte - avatar
+ 4
You don not need 2nd condition in while(!sc...) Actually the second condition sc.next() moves the Scanner object to the next line. So it gives Scanner error. If you let the code as it is and give any input in first line followed by the 2 required inputs (total 3 inputs) it will work.
13th Oct 2017, 6:29 AM
Meharban Singh
Meharban Singh - avatar
+ 4
@Meharban Thanks a bunch, I see what you mean now
13th Oct 2017, 6:32 AM
Mark Paramonte
Mark Paramonte - avatar
+ 4
Or remove sc.next() == ""
13th Oct 2017, 6:32 AM
Meharban Singh
Meharban Singh - avatar
+ 4
Anytime :-) @Mark Paramonte
13th Oct 2017, 6:33 AM
Meharban Singh
Meharban Singh - avatar
+ 4
Yes
13th Oct 2017, 6:38 AM
Meharban Singh
Meharban Singh - avatar
+ 4
I just found that with first input 3, second input 1, I get the issue where winner is not declared again :(
13th Oct 2017, 6:43 AM
Mark Paramonte
Mark Paramonte - avatar
+ 4
fixed. I'm sorry for not catching that. I see what you mean.
13th Oct 2017, 6:48 AM
Mark Paramonte
Mark Paramonte - avatar
+ 3
Thank you. That DOES fix the problem I'm having, BUT it also causes a bug where the dead fighter attacks 1 more time after its health reaches <=0
13th Oct 2017, 4:57 AM
Mark Paramonte
Mark Paramonte - avatar