Newbie- Help with code


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Newbie- Help with code

  1. #1
    Join Date
    Jun 2004
    Posts
    3

    Newbie- Help with code

    Hi,

    I've been working on this code for a hangman game but have got a problem with it. The problem is that it only checks for the last letter of the secrect word.

    Thanks in advance,

    John.

    public class HangmanGuts extends Frame
    {
    {
    String CurWord = "";
    String dashes = "";
    int guesses;
    guesses = 0;
    int j;
    int a;
    int counter;
    counter = 0;
    String curChar = "";
    String curCharDash = "";
    String UserGuess;
    String words[]={"Mountain", "Mole", "Locker", "Routier", "Grim", "Insult", "Indepedent", "Sandbag", "Proposal", "Cricket"};

    a = (int)(Math.random() * 10);
    CurWord = words[a];
    dashes = "";
    for (int i = 1; i<=CurWord.length(); i++)
    dashes +="-";
    System.out.println(CurWord);
    System.out.println("Welcome to Hangman, type a letter and press enter");

    for (int c=0; c<=10; c++)
    {

    UserGuess = "";
    UserGuess = EasyIn.getString();

    for (j=0; j <CurWord.length()-1; j++);
    {
    curChar = String.valueOf(CurWord.charAt(j));
    curCharDash = String.valueOf(dashes.charAt( j ));

    if(curChar.equalsIgnoreCase(UserGuess)&& curCharDash.equals("-"))
    dashes = dashes.substring(0,j)+UserGuess+dashes.substring(j+1);
    }

    System.out.println(dashes);

    if(counter == 10)
    {
    System.out.println("You lose!");
    System.out.println("The word was "+CurWord);
    counter = 0;
    break;
    }
    else if(dashes.equalsIgnoreCase(CurWord))
    {
    System.out.println("You win!");
    counter = 0;
    break;
    }
    }

    }
    }

    public class runHangman
    {
    public static void main(String args[])
    {
    HangmanGuts a = new HangmanGuts();
    }

    }

  2. #2
    Join Date
    Sep 2004
    Posts
    3
    You have a ';' after the for loop...

    for (int j=0; j <CurWord.length()-1; j++);
    {

    i reckon that could be it.
    Also, you may want to lose the '-1' part from that loop or it wont check the final letter (as far as i can tell).
    Hope that fixes everything.

  3. #3
    Join Date
    Jun 2004
    Posts
    3

    Red face

    Thanks for you help, I spent 2 hours scatching my head wondering why it wouldn't work

    John.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center