Hiya, I need to make a game for my cs class.
My game is the word find game and it plays like this:
You are given a long word like "antidisestablishmentarianism"
and your goal is to find as many words in to word given as possible.
My program will save the word inputed and check if they have been used before and check if the word inputed is inside the word given.
My game currently compiles but is missing some key ingredients like a loop to repeat
JOptionPane.showInputDialog("Please Enter a String");
asking the user to input words.

public class WordFind
{

    public static void main(String[] args)
    {
        String word = "antidisestablishmentarianism"; 
        System.out.println(word);
        String input = JOptionPane.showInputDialog("Please Enter a String");
        WordsEntered game = new WordsEntered();
                
        if (game.check(input)) //checks if input was used before
        {
            for (int i = input.length()-1; i == 0; i--) //gets the last letter from input 
            {
                for (int c = word.length()-1; c == 0; c--) //gets the last letter from word
                {
                    if ((input.substring(i)).equals(word.substring(c))) //checks input's last letter with word's last letter 
                    {
                        if (input.substring(0).equals(word.substring(0))) //checks if the checking letters if done then input is added
                        {
                        game.add2Array(input);
                        game.lastWord();
                        }
                    }
                    else
                    {
                    System.out.println("Invalid Word:" + input.substring(i) + "is not in word");
                    break;
                    }
                }
            }
        }
        else
        {
        System.out.println("Invalid Word: input found in wordsUsed");
        }
    }
}
public class WordsEntered
{
    ArrayList<String> wordsUsed;

    public WordsEntered()
    {
        wordsUsed = new ArrayList<String>();
    }

    public void add2Array(String input) // adds word to wordsUsed array
    {
        wordsUsed.add(input);
    }

    public String lastWord()
    {
        return wordsUsed.get(wordsUsed.size()-1);
    }

    public boolean check(String input)
    {
        Iterator<String> itr = wordsUsed.iterator();

        while(itr.hasNext())
        {
            if (itr.next().equals(input))
            {
                return false;
            }
        }
        return true;
    }
}

Recommended Answers

All 6 Replies

Do you have any specific questions, like how to code in java to do something?
Or what is this error?

So the point of you taking a cs class and having us write the code for you is what exactly? :)

Wrap the code that you want to loop in a while statement. This is an indefinite loop, you're going to go until the user's done playing. You can handle that two ways, either you can do

while (true)
{
....blah blah...
if (done)  
{
break;
}
}
boolean done = false;
while (!done)
{
....blah blah...

}

in either case, you use some input from the user to decide when they've had enough. Maybe they enter an empty line, or some sentinel value, or what have you.

Hope this helps.

i was debugging my program and found out that:
for (int i = input.length()-1; i == 0; i--)
doesnt execute.

doesnt execute.

Please explain your meaning? How can it not execute?
Do you mean that the if condition fails and the code doesn't go into the statement following the if() statement?
When is the conditional part of the for statement true? And when is it false? What would cause the if to not iterate?
What is the value of input.length()?

It executes. It gets input.length()-1, it compares it to 0, finds the condition false, so it moves on. It'll do whatever's in the loop one time if the length of "input" is exactly 1.

That's not what you had in mind, is it?
Remember, it'll execute the loop as long as the loop condition is true.

Hmm... This post is from the same person but the code looks a little bit different... The loop will never be executed properly because..

// this line of code does NOT get the last letter from input
// but it is looping from the last letter until the first letter
for (int i = input.length()-1; i == 0; i--) //gets the last letter from input

// same thing happens in this line as well
for (int c = word.length()-1; c == 0; c--) //gets the last letter from word

After reading your original post again, the key word of the assignment is...

You are given a long word like "antidisestablishmentarianism"
and your goal is to find as many words in to word given as possible.

I don't see how your program find a 'word' from the long string??? Are you supposed to check whether the input word is inside your long string??? What are you trying to accomplish??? I think you may not be doing it the way the game is supposed to be. Here is my guess... You are supposed to have a predefine word storage. Use your word storage to check against the long string???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.