Results 1 to 7 of 7
  1. #1
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Lightbulb I need help with this code

    hi

    I'm using bluej

    I'm making a word guesser and I really need help with it

    I need the computer to pick a word from the arraylist and than try to guess it letter by letter or even guess the whole word.

    this is my code so far with some errors so please help:
    Java Code:
    import java.util.*;
    
    // bluej is actually a bad name of a class because it should start with capital letter.
    public class WordGuesser {
    
    public static void main(String[]args) {
    System.out.println("start");
    ArrayList words = new ArrayList();
    
    
    words.add("bob");
    words.add("tony");
    words.add("steve");
    words.add("justin");
    words.add("jeff");
    words.add("mark");
    words.add("matt");
    words.add("dan");
    words.add("paul");
    words.add("jim");
    
    String randomWord = words.get(Math.random() * words.size());
    boolean isCorrect = false;
    while(!isCorrect) {
    System.out.println("What word am I thinking of?");
    String guess = ""; // read in guess using System.in
    
    isCorrect = randomWord.equals(guess);
    if(!isCorrect) {
    System.out.println("Sorry that wasn't correct.");
    } else {
    System.out.println("That's correct. I was thinking of " + randomWord);
    }
    } 
    System.out.println("finish");
    }
    }


    in this line there is an error "String randomWord = words.get(Math.random() * words.size());"

    this is the error:

    cannot find sympole - method.get(double)

    I'm new to java so please help me

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    If you look at the List API here...

    List (Java 2 Platform SE v1.4.2)

    you'll notice there is no method get(double) only get(int).

    (Math.random() * words.size()) is returning a double not an int, so you'll need to type cast it into an int.

  3. #3
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks

    what about the ret of the code is it doing the right thing

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Currently it looks good if you are trying to guess the whole word. If you want to guess it letter by letter you could iterate through all the characters in the word and see if any match. I'm not sure what you are looking for if they match, so I wouldn't know how to handle it the match is recognized.

  5. #5
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    the game I'm trying to make is like hangman but the player is the computer

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    So are you trying to make the computer guess the word or yourself?

    If the computer is only picking the words and you are trying to guess them, you could do a check for the input and see if its only one character long. If so you can deduce its just trying to guess the letters in the word and you can do a check for the matches and return the indexes that the character is at. If the input is more then one character long, then it would be a word guess, and you can see if the whole word matches.

  7. #7
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    yah I'm trying to make it for the computer to guess the word

    the code is working now but if the computer doen't guess the word it will stay trying to guess it can you help me do a limit to the number of tries the computer can have

    thank you

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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