My objective is make a game program for my cs class. So my game is like this:
You input a word and then input another word which starts from the last letter of the first word. The program check if the word has been used before and if the last letter matches the first letter. My bugs are:
editing/incrementing string names ex: (word + "i++")
line of code for last letter in a string ex: equals(words.substring(last)
very noob question: in an arraylist: what is words[0]? correct syntax
does this

(word + "i++").equals(words)

compare wordx to each and every variable in the arraylist words?

import javax.swing.JOptionPane;
import java.util.ArrayList;

public class Words
{

    public static void main(String[] args)
    {
        ArrayList<String> words = new ArrayList<String>();
        String word1 = JOptionPane.showInputDialog("Please Enter a String");
        WordCheck game = new WordCheck(words);
        game.add2Array(word1);
        System.out.println(word1);

        for (int i = 0; game.condition(true); i++)
        {
            String (word + "i++") = JOptionPane.showInputDialog("Please Enter a String");
            if (word2.substring(0).equals(words[i].substring(last)) || (word + "i++").equals(words))
            {
                game.condition(false);
            }
            else
            {
                game.add2Array(word + "i++");
                System.out.println(word + "i++");
            }
        }

        System.out.println("Game Over!");
    }

}
import java.util.ArrayList;

public class WordCheck
{
    ArrayList<String> wordsUsed;

    public WordCheck(ArrayList<String> words)
    {
        words = wordsUsed;
    }
    
    public void add2Array(String word1)
    {
        wordsUsed.add(word1);
    }

    public boolean condition(boolean value)
    {
        return value;
    }

}

In the WordCheck constructor you have it backwards. You are suppose to set the value of the class attribute wordsUsed. Instead you change the argument and the wordsUsed attribute of the WordCheck never takes value and it is null.

Also what is the purpose of the condition method ? It will always return the argument, so I don't see a purpose there. In the for loop it will always return true and the loop will never end

Edited 6 Years Ago by javaAddict: n/a

for (int i = 0; game.condition(true); i++)

is wrong
correct is

for (int i = 0; game.getCondition(); i++)
public class WordCheck
{
    ArrayList<String> wordsUsed;
private condition = true;

    public WordCheck(ArrayList<String> words)
    {
        words = wordsUsed;
    }
    
    public void add2Array(String word1)
    {
        wordsUsed.add(word1);
    }

    public void setCondition(boolean value)
    {
        this.condition = value;
    }
     public boolean getCondition(){
     return condition;
}


}

You can't create variables like "i++". You have to use arrays.

I fixed the bugs of condition and wordsused. I still have the bugs as before:
word[i++] //new varible name each time
how do i initialize an arraylist varible words[0]? correct syntax
how do i extract the last letter of words using .substring(?last?)

public class Words
{

    public static void main(String[] args)
    {
        ArrayList<String> words = new ArrayList<String>();
        String word1 = JOptionPane.showInputDialog("Please Enter a String");
        WordCheck game = new WordCheck(words);
        game.add2Array(word1);
        System.out.println(word1);

        for (int i = 0; game.getCondition(); i++)
        {
            String word[i++] = JOptionPane.showInputDialog("Please Enter a String");
            if (word[i++].substring(0).equals(words[i].substring(last)) || (word + "i++").equals(words))
            {
                game.setCondition(false);
            }
            else
            {
                game.add2Array(word[i++]);
                System.out.println(word[i++]);
            }
        }

        System.out.println("Game Over!");
    }

}
public class WordCheck
{
    ArrayList<String> wordsUsed;
    private boolean condition = true;

    public WordCheck(ArrayList<String> words)
    {
        wordsUsed = words;
    }
    
    public void add2Array(String word[])
    {
        wordsUsed.add(word[]);
    }

    public void setCondition(boolean value)
    {
        this.condition = value;
    }
    
    public boolean getCondition() 
    {
        return condition;
    }

}

new code. i made it cleaner with one array but still dont know how to extract the last letter from a string and am having trouble with comparing a string to every string an arraylist.

public class Words
{

    public static void main(String[] args)
    {
        boolean gameCondition = true;

        String word = JOptionPane.showInputDialog("Please Enter a String");
        WordsStorage game = new WordsStorage();
        game.add2Array(word);
        System.out.println(word);

        while (gameCondition)
        {
            String word1 = JOptionPane.showInputDialog("Please Enter a String");
            if (word1.substring(0).equals(word.substring(15)) || (word1).equals(game.wordsArray()))
            {
                gameCondition = false;
            }
            else
            {
                game.add2Array(word1);
                game.lastWord();
            }
        }

        System.out.println("Game Over!");
    }

}
public class WordsStorage
{
    ArrayList<String> wordsUsed;

    public WordsStorage()
    {
        wordsUsed = new ArrayList<String>();
    }
    
    public void add2Array(String word)
    {
        wordsUsed.add(word);
    }

    public String lastWord()
    {
        return wordsUsed.get(wordsUsed.length()-1); //fix this
    }

    public ArrayList wordsArray()
    {
        return wordsUsed;
    }

}

how to extract the last letter from a string

If you know how long the string is, you could get the character at its end by using the methods available in the String class.

If you know how long the string is, you could get the character at its end by using the methods available in the String class.

i just got it from you reply.
if (word1.substring(0).equals(word.substring(word.length()))
// if word1 first letter doesnt equals the length of the word2
// exit
what is the negation of .equals?

now that ^ fixed almost how do i compare a string to every string in an array?
ex: (word1).equals(game.wordsArray())
//wordsArray just return the arraylist
// netbeans complains^ that you cannot comapre a string with an arraylist

if (word1.substring(0).equals(word.substring(15))

As NormR1 said, does your enter string always length 16? If you want to check for the last character, use the string.substring(string.length()-1) instead. NOTE that string.length()-1 because it is 0-index. In other words, the first character is at position 0, so the last position is at length()-1.

(word1).equals(game.wordsArray()))

This is also wrong. You cannot compare a String class with ArrayList class just by using equals() method. What are you trying to do here? If you want to check whether the 'word1' is already used, you need to iterate through your array and check whether the 'word1' String is equal to any of String inside your game.wordsUsed.

game.lastWord();

What is the purpose of using this? The method is returning a String, but you just use it without any variable assigned.

// change this line --> return wordsUsed.get(wordsUsed.length()-1); to...
return wordsUsed.get(wordsUsed.size()-1);

If you are not familiar to using Java class, check this Java 6 API Doc for further information.

If you do not know how to iterate through an ArrayList to check what inside, a sample code snippet is below. Just to give you an idea. I am not exactly sure about your purpose here...

public boolean isInclude(String str) {
  Iterator<String> itr = wordsUsed.iterator();  // get the array list iterator
  while (itr.hasNext()) {  // check whether the array list has the next element
    if (itr.next().equals(str)) { return true; }  // found equal string, return true
  }
  return false;  // gone through the whole list and not found any equal string, false
}

// and you use it in your main as game.isInclude(word1);

Edited 6 Years Ago by Taywin: n/a

The ArrayList class has some methods that may be useful for looking at its contents.
Read the API doc to see.

thanks taywin your suggestions helped alot. there is my revised code there are still some bugs like making
word1.substring(0).equals(word.substring(word.length()-1)
say NOT equal.

public class Words
{

    public static void main(String[] args)
    {
        String word = JOptionPane.showInputDialog("Please Enter a String"); // user enters string called word
        WordsStorage game = new WordsStorage(); // makes objects game and new array wordsUsed in wordsStorage class to keep track of wordsused 
        game.add2Array(word); // adds word to wordsUsed
        System.out.println(word); // prints word

        boolean gameCondition = true; 
        while (gameCondition)
        {
            String word1 = JOptionPane.showInputDialog("Please Enter a String"); // user enter string which should start from the last letter from the last word: word
            if (word1.substring(0).equals(word.substring(word.length()-1)) || game.isInclude(word1)); // checks if the word1's first letter matches word's last letter and checks if word1 was used before 
            {// i need to negate    ^ to not equals. so if first letter of word1 not equal to last letter GAME OVER
                gameCondition = false; // if the last letter doesnt match first or word was used before game over
            }
            else
            {
                game.add2Array(word1); //adds word1 to array and repeats
                game.lastWord(); //prints last word inputed
            }
        }

        System.out.println("Game Over!");
    }

}
public class WordsStorage
{
    ArrayList<String> wordsUsed; // making an array to store string used

    public WordsStorage()
    {
        wordsUsed = new ArrayList<String>(); 
    }
    
    public void add2Array(String word) // adds word to wordsUsed array
    {
        wordsUsed.add(word);
    }

    public String lastWord() //prints last word entered
    {
        return wordsUsed.get(wordsUsed.size()-1); //fix this
    }
    
    public boolean isInclude (String str) //checks if word enter has been used before in wordsUsed
    {
        Iterator<String> itr = wordsUsed.iterator();
        
        while(itr.hasNext())
        {
            if (itr.next().equals(str)) 
            {
                return true;
            }
        }
        return false;
    }
    
}

Edited 6 Years Ago by imag1ne: n/a

I am going to use
(! (word1.substring(0).equals(word.substring(word.length()-1)))
to mean
word1.substring(0).equals(word.substring(word.length()-1)
is NOT equal.

i am using
(! (word1.substring(0).equals(word.substring(word.length()-1))
to mean
word1.substring(0).equals(word.substring(word.length()-1)
doesnt equal. So that when the last letter doesnt match the first the game stops.

oops didn't read the whole answering... Need to edit. You are correct to use exclamation as negation.

Edited 6 Years Ago by Taywin: n/a

I am down to one bug left. My 'else' give an error 'else without if'.
Also I think that my variable name for inputing strings should change each time or else it will overwrite it.

public class Words
{

    public static void main(String[] args)
    {
        String word = JOptionPane.showInputDialog("Please Enter a String"); // user enters string called word
        WordsStorage game = new WordsStorage(); // makes objects game and new array wordsUsed in wordsStorage class to keep track of wordsused
        game.add2Array(word); // adds word to wordsUsed
        System.out.println(word); // prints word

        boolean gameCondition = true;
        while (gameCondition)
        {
            String word1 = JOptionPane.showInputDialog("Please Enter a String"); // user enter string which should start from the last letter from the last word: word
            if (word1.substring(0).equals(word.substring(word.length()-1)) || game.isInclude(word1)); // checks if the word1's first letter matches word's last letter and checks if word1 was used before
            {
                game.add2Array(word1); //adds word1 to array and repeats
                game.lastWord(); //prints last word inputed
            }
            else // I get an error here: else without if???
            {
                gameCondition = false; // if the last letter doesnt match first or word was used before game over
            }
        }

        System.out.println("Game Over!");
    }

}
public class WordsStorage
{
    ArrayList<String> wordsUsed; // making an array to store string used

    public WordsStorage()
    {
        wordsUsed = new ArrayList<String>();
    }
    
    public void add2Array(String word) // adds word to wordsUsed array
    {
        wordsUsed.add(word);
    }

    public String lastWord() //prints last word entered
    {
        return wordsUsed.get(wordsUsed.size()-1); //fix this
    }

    public boolean isInclude (String str) //checks if word enter has been used before in wordsUsed
    {
        Iterator<String> itr = wordsUsed.iterator();

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

}

Why does the compiler not see the connection with the if statement?
Look closely to see what is ending the if statement before the else.

if (word1.substring(0).equals(word.substring(word.length()-1)) || game.isInclude(word1));

Look at the ending of this line. If you use semicolon, it means you want to end the statement instead of use it as a block. Delete the semicolon.

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.