Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with java char Array

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help with java char Array

    Working on a Hangman Game. Trying to add a guess(char variable) into char array and display a list of guessed letters. Please help this piece code isn't working

    while(guessedWord.toString().equals(test.toString( )) == false && errors < 5)
    		  {  
    		  	 System.out.print("Guess Letter: ");
              Scanner scan = new Scanner(System.in);
              guess = scan.next().charAt(0);
    			 char[] guessedLetters = new char [10];           //THE PROBLEM
     
    			 System.out.println(guessedLetters);                //THE OTHER PROBLEM
     
    			 //System.out.println("Letters Already Used: "+ lettersGuessed[0]);
              for(int i = 0; i < guessedWord.length(); i++){	 //check if letter has been guessed		 	
     
    			 if(guessedWord.charAt(i) == guess)
    			 {       
    			 		test.setCharAt(i, guess);
    					System.out.println(test);
    			 }
     
    			 			   											 }
    			 if(word.indexOf(guess) == -1 )          //if letter not in secret then errors goes up by 1 and while loop continues
    			 {
                     errors++;
    			 }
     
    			 if(guessedWord.toString().equals(test.toString( )) == true)
    			 {
    			 	score = (5 - errors);
    				cum += score;
    				System.out.println("\nYou win !!!");
    				System.out.println("Your cumlative score is: " + cum);
    				initGame();
    			 }
     
    			 if(errors == 5)
    			 {
    				String name;
    				Scanner input = new Scanner(System.in);
    			 	System.out.println("\nYou lose !!!");
    				System.out.println("Your score is: " + cum);
    				System.out.print("Please enter your name: ");
    				name = input.nextLine();
    				myWord.setSaveScore(name, cum);
    				cum = 0;
    				initGame();
    			 }


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Windsor, Ontario
    Posts
    9
    My Mood
    Bored
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with java char Array

    If I were you I'd start with some pseudo code and then start programming it and if you run into any errors it would be a lot easier to help.

    Something like:
    While loop to keep the user entering until ran out of guesses and you could use a counter to keep track of number of guesses
    Going to need a scanner to get the input from keyboard
    int x; // storage variable
    For loop to access all the index's in the array
    x = Integer.parseInt(s); (Trying to give a hint)
    Let the index of the loop equal x

    Hope this helps.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with java char Array

    static void guess(String secret, int times) {
    // read character from console
    Scanner reader = new Scanner(System.in);

    int secretLen = secret.length();
    int timesCount = 0, index = 0;

    while (timesCount++ < times) {
    System.out.println("Enter the letter you want guess: ");
    char incomingLetter = reader.next().charAt(0);
    char targetLetter = secret.charAt(index);
    if (incomingLetter == targetLetter) {
    System.out.println("You got it, please guess the next");
    index++;
    if (index >= secretLen)
    break;
    } else {
    System.out.println("Wrong, the letter is not matched, repeat!");
    }
    }

    if (index >= secretLen) {
    // guess operation succeed
    System.out.println("You win");
    }
    }

    This is what I do to guess the letter. And the print things is changeable for you to fulfill your specific target!
    to do or not to do is a question

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Windsor, Ontario
    Posts
    9
    My Mood
    Bored
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with java char Array

    Ah I'm glad to see you revised your post haha!

    I don't think you can just print guessedLetters like you have.

    Try this:
    import java.util.Arrays;
    .
    .
    .
    System.out.println( Arrays.toString( guessedLetters ) );

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with java char Array

    Doesn't really answer my intended question. If you have a look at my code above. Im having problems when displaying all the guessed letters. As in when user guesses it stores in array then displays it

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM
  3. [SOLVED] Char Array Increment + 1
    By Nuggets in forum What's Wrong With My Code?
    Replies: 38
    Last Post: April 13th, 2012, 05:35 AM
  4. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  5. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM

Tags for this Thread