Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Location
    Philly
    Posts
    3
    Rep Power
    0

    Default Array traversal issues

    I am trying to make a simple hangman program, and I'm working on getting the framework set up. The problem is, I'm pretty sure, in the if statements in the compare method the int whichletter is supposed to be incremented in each run through the for loop in the main method (8 times, it should be) so that it reads the next letter of the randomly chosen 8-letter word, but I think it's just staying at charAt(0) for some reason.. here is the code..

    note: I know there are other problems with it probably, but I'm just kind of stuck on this one right now.

    Java Code:
    import java.io.*;
    import java.util.*;
    public class stuff {//begin classbody
    public static void main (String[]args)throws IOException
    {//BEGIN MAIN
       
    
        String wordchosen = PickRandomWord();
        System.out.println(wordchosen);
        int whichletter =0;
        
        for (int cnt1=0; cnt1<8; cnt1++)
        {
        String guessedletter=guess();
        System.out.println("you guessed " + guessedletter);
        compare(wordchosen, guessedletter, whichletter);
        whichletter++;
        }
       
       
       
    }//END MAIN
    
    
    public static String PickRandomWord()
    {//begin pickrandom
        //words to be picked from
        String wordchosen;
        String [] word = new String [5];
        word [0] ="aardvark";
        word [1] ="trioxide";
        word [2] ="chromium";
        word [3] ="lungworm";
        word [4] ="zombiism";
       
       
        //generate a random number
        Random randomword = new Random();
        int random = randomword.nextInt( 5 );
    
        wordchosen = word[random];
       
        return wordchosen;
    }//end pickrandom
    
    public static String guess() throws IOException
    {//BEGIN GUESS
        BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Please Enter your guess. (all lowercase, please)");
        String guessedletter = in.readLine();
       
        return guessedletter;
    }//END GUESS
    
    public static void compare (String wordchosen, String guessedletter, int whichletter)
    {//BEGIN COMPARISON
        int right = 0;
        int wrong = 0;
        int length = 8;
    
    
    	if (guessedletter.charAt(whichletter) == wordchosen.charAt(whichletter))
    	{
    		right++;
    	}
    
    	if (guessedletter.charAt (whichletter) != wordchosen.charAt(whichletter))
    	{
    		wrong++;
    	}
    
    System.out.println("You guessed " + wrong + " letters incorrectly;.");
    
    System.out.println("You guessed " + right + " letters correctly;.");
    
    }//END COMPARISON
    
    
    }//end class body
    Help appreciated!
    -Sondra

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Instead of using your whichletter int, you could just use the cntl int that you are using to control your for loop. This should insure that your number is incrementing.

    Also, in your if statements
    Java Code:
    if (guessedletter.charAt(whichletter) == wordchosen.charAt(whichletter))
    	{
    		right++;
    	}
    I think it should be

    Java Code:
    if (guessedletter.charAt(0) == wordchosen.charAt(whichletter))
    	{
    		right++;
    	}
    Unless I am mistanken, the guessedLetter is overwritten each time and only contains a single character correct?

  3. #3
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Array traversal issues

    the problem seems to be right + wrong. the way you have it set up at the moment, testing each letter, neither can = more than 1

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Bidirectional Traversal with ListIterator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:37 PM
  2. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:16 AM
  3. Array issues
    By Neo82 in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 04:22 AM
  4. JTable Focus Traversal
    By helios_lie in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2007, 11:27 AM
  5. Preorder Traversal problem...
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-04-2007, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •