whats the problem with this hangman? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

whats the problem with this hangman?

run the program and see whats the problem... the think that causes the problem are the spaces is the array... code: import java.util.*; public class Class1 { public static void main(String[] args) { System.out.println("Enter a Character/letter:"); String word = "hangman"; char[] charArr = word.toCharArray(); int tries = 20; String blank = ""; for (int i = 0; i < word.length(); i++){ blank = blank + "_ "; } char[] blankCharArr = blank.toCharArray(); System.out.println(blank); System.out.println(""); Scanner scan = new Scanner(System.in); char aTry; while (tries > 0){ aTry = scan.next().charAt(0); for (Character j: charArr){ if(j.equals(aTry)){ int indexOfChar = word.indexOf(aTry); if (blankCharArr[indexOfChar] == '_'){ blankCharArr[indexOfChar] = aTry; }else if (blankCharArr[indexOfChar] == ' ' && blankCharArr.length -1 > indexOfChar){ blankCharArr[indexOfChar +1] = aTry; } System.out.println(String.valueOf(blankCharArr)); break; } } tries++; } if (tries == 0){ System.out.println("You are out of tries!"); }else{ System.out.println("Congrats!"); } } }

18th Sep 2020, 7:17 PM
Yahel
Yahel - avatar
11 Answers
+ 2
your code appears to use a while loop to interact with user input. it's not supported on Sololearn. Sololearn playground is not an interactive console. it compiles everything on the server side and send you the results, so there is no way for the user to enter another input.
18th Sep 2020, 7:37 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
I looked it and I didn't know the problem description.. Add it.. I try. As already specified by @Bahaha indexOf() method find first occurrence only.. Add what is required output...?
19th Sep 2020, 2:46 PM
Jayakrishna 🇮🇳
+ 1
Without description, I have to assume only.. Then I assuming you are replacing Word matches in empty string of hyphon a s spaces... If that is what you trying... Then If I enter h a n g then after h a successfully adds but int indexOfChar = word.indexOf(aTry); This will return for n of index 2, but at index 2 or 3, there are no more _ hyphons at index 0 to 3 it is after index 3. So it fails to add n. You may need to find blankCharArr of next hyphon directly.. I think. If I understood your problem...!!!!!!
19th Sep 2020, 3:16 PM
Jayakrishna 🇮🇳
0
Bahha🐧 I coded this on eclipse... this is not the problem..
18th Sep 2020, 7:59 PM
Yahel
Yahel - avatar
0
I didn't go through the code fully, I just saw that you are using a while loop to interact with user input, and that doesn't work here. are you saying that you are not running your code on Sololearn?
18th Sep 2020, 8:03 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Bahha🐧 I tried doing it on sololearn but it didnt work so I downloaded eclipse and tried it there and it worked... The problem that I got into is above the code..
18th Sep 2020, 8:05 PM
Yahel
Yahel - avatar
0
ok I checked your code, the issue is with the space after _ in blank = blank + "_ "; remove it blank = blank + "_"; + using word.indexOf(aTry); is not going to work for duplicate letters.
18th Sep 2020, 9:24 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Bahha🐧 , but i did the spaces because i want it to look estatic and nice.. if i wont do them: all the underscores will combine and you wont be able to count how many letters are in the word. how can i make it work? without space: _____ with space: _ _ _ _ _
19th Sep 2020, 11:45 AM
Yahel
Yahel - avatar
0
instead tries++, add tries--;
19th Sep 2020, 2:40 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 oh.. my bad.. thanks. But this is not the main problem.. look at title and try playing it
19th Sep 2020, 2:42 PM
Yahel
Yahel - avatar
0
The problem is the spaces in the array. If you try playing it to the end you will see a problem. I don't know how to describe it, you just need to see it by yourself. Its something with the spaces in the array.
19th Sep 2020, 2:49 PM
Yahel
Yahel - avatar