Java compare user input to char array -


first off hello, new here. i'm new java , i'm having issues comparing user input using scanner class dynamically created char array. have list of 10 words, program randomly choosing one, converting char array. i've got working. i'm having issue comparing user input char in array. i'm practicing string manipulation , creating simple hang man game daughter can fool with. advice appreciated, helpful links information great too. in advance.

import java.util.random; import java.util.scanner;  public class hangman {      public static void main(string[] args) {          string[] words = new string[10];          words[0] = "elsa";         words[1] = "anna";         words[2] = "olof";         words[3] = "sphen";         words[4] = "christoph";         words[5] = "tinkerbell";         words[6] = "arial";         words[7] = "snowwhite";         words[8] = "cinderella";         words[9] = "sleepingbeauty";          random selword = new random();         int newword = selword.nextint(10);          char[] wordletters = words[newword].tochararray();         scanner userinput = new scanner(system.in);          system.out.println("please make first guess");          (int i=0; < words[newword].length(); i++) {             system.out.print("_ ");         }          if (!userinput.hasnextline()) {             {                 system.out.println("please enter letters only.");             } while (!userinput.hasnext());         }          (int n=0; n < wordletters.length; n++) {             string userchoice = userinput.nextline();              if (wordletters[n] == userchoice.charat(n)) {                 system.out.println("you've made match");             } else {                 system.out.println("sorry, try again.");             }         }          userinput.close();          // system.out.println(words[newword]);         // system.out.println(words[newword].length());      } } 

the problem expecting user input character @ each line, compare userchoice.charat(n). should compare character @ userchoice.charat(0).

change this:

if (wordletters[n] == userchoice.charat(n)) 

to:

if (wordletters[n] == userchoice.charat(0)) 

in addition this, think check below should inside for loop before string userchoice = userinput.nextline():

if (!userinput.hasnextline()) {         {             system.out.println("please enter letters only.");         } while (!userinput.hasnext());     } 

so for loop should this:

for (int n=0; n < wordletters.length; n++) {         while (!userinput.hasnextline()) {             system.out.println("please enter letters only.");         }         string userchoice = userinput.nextline();          if (wordletters[n] == userchoice.charat(0)) {             system.out.println("you've made match");         } else {             system.out.println("sorry, try again.");         }     } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -