0

Example:

 wordsUsed[0][0] = "Word 1";
 wordsUsed[0][1] = "0";
 wordsUsed[1][0] = "Word 2";
 wordsUsed[1][1] = "0";

 String wordsCopy[][] = new String[2][2]

What I want is that wordsCopy[][] contains "Word 1" 2 times and "Word 2" also 2 times. what I don't want is random amount of times of "Word 1" and random amount of times "Word 2"

wordsUsed[x][0] 

^is a string with a word in it // x meaning 'any value'

wordsUsed[x][1] 

^is standard "0"

wordsCopy[][] 

^is a new array that will store 2 of each string in kleurGebruikt[x][0]

Basicly what I'm trying to accomplish is that the same string from [x][0] is never stored more than 2 times in the new array

What I got so far is that it will check the counter and if it is 0 it will copy the string over and then change the 0 to 1. if it gets the same string later it will see that it is already used once and copy it once more. Now my problem is that when it is copied twice, I'm unsure of how to make it skip this string and try a different one. Any help is appreciated

public void randomWords() {
    Random r = new Random();
    int rndm = 0;

    for(int i=0; i<4; i++){
        for(int j=0; j<5; j++){
            rndm = r.nextInt(wordsUsed.length);
            if (wordsUsed[rndm][1].equals("0")){
                wordsCopy[i][j] = wordsUsed[rndm][0];
                wordsUsed[rndm][1] = "1";
            }

            else if (wordsUsed[rndm][1].equals("1")){
                wordsCopy[i][j] = wordsUsed[rndm][0];
                wordsUsed[rndm][1] = "2";
            }                

            else if (wordsUsed[rndm][1].equals("2")){
                 // at this point I need to go back to the start of the current 
                 // if loop and make it so that a new random string is searched for
                 // without leaving a null element in the array.
            }  
}

Btw I'm sorry if the title isn't really good. I wasn't really sure how to explain myself in just a few words

Thanks

10
  • try this replace line 'else if (kleurGebruikt[rndm][1] == "1"){' with : 'if (kleurGebruikt[rndm][1] != "0"){' ..... then increment kleurGebruikt[rndm][1] by ONE Commented Aug 25, 2013 at 17:07
  • 1
    You should use equals to compare strings. Other than that, please explain what you want with an example. Commented Aug 25, 2013 at 17:08
  • @bsd I have added an example in the main post Commented Aug 25, 2013 at 17:13
  • @SrinathGanesh The problem is that it will detect that it already has 2 of the same, but it will leave the space in the array open instead of looking for a new word to put in there. Commented Aug 25, 2013 at 17:15
  • I have never seen such variable names. I am not sure but, what you could do is collect non-zero strings from kleurGebruikt in a list and then duplicate it as many times in your second 2D array. Commented Aug 25, 2013 at 17:17

2 Answers 2

1

Use equals to compare strings. So, instead of-

if (kleurGebruikt[rndm][1] == "0"){
    ...
}

Should be-

if("0".equals(kleurGebruikt[rndm][1])){
    ...
}

EDIT:

I had hard time understanding your code because of those variable names and the design. Here's a way you can do it. If I were you I wouldn't use array for this task though-

Random r = new Random();
int rndm = 0;

String[][] wordsUsed = new String[2][2];
wordsUsed[0][0] = "Word 1";
wordsUsed[0][1] = "0";
wordsUsed[1][0] = "Word 2";
wordsUsed[1][1] = "0";

String wordsCopy[][] = new String[2][2];

int ctrR = 0;
int ctrC = 0;
boolean isDone = false;

while(!isDone) {

    rndm = r.nextInt(wordsUsed.length);

    if (wordsUsed[rndm][1].equalsIgnoreCase("0") 
            || wordsUsed[rndm][1].equalsIgnoreCase("1")){

        if (wordsUsed[rndm][1].equalsIgnoreCase("0")){
            wordsCopy[ctrR][ctrC] = wordsUsed[rndm][0];
            wordsUsed[rndm][1] = "1";
        } else {
            wordsCopy[ctrR][ctrC] = wordsUsed[rndm][0];
            wordsUsed[rndm][1] = "2";
        }     

        if(ctrC == wordsCopy.length - 1){
            ctrR++;
            ctrC = 0;
        } else { 
            ctrC++;
        }
    }

    if(ctrR == wordsCopy.length){
         // If ctrR reached the length, we have successfully completed copying
        isDone = true;
    }    
}           


///////////////// RESULTS /////////////////

// Values of wordsUsed array
for(int i = 0; i < wordsUsed.length; i++){
    for(int j = 0; j < wordsUsed.length; j++){
        System.out.print(wordsUsed[i][j] + "\t");

    }
}

System.out.println("\n");

// Values of wordsCopy array    
for(int i = 0; i < wordsCopy.length; i++){
    for(int j = 0; j < wordsCopy.length; j++){
        System.out.print(wordsCopy[i][j] + "\t");

    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I've already changed this, as requested in comments of OP. However this does not fix my problem.
The thing is, I need all 'spots' in my array to be filled, so I can't just break it off. It needs to restart the procedure to assign a word on the same spot where it stopped when there were 2 of the word in the array already
@JentevanHeuverswyn Check my updated answer. This should work.
I can't currently test it out but I've read through the code and I see what you're going for. I'm pretty sure this will do the trick for me so I will mark this as solved. Thanks!
1

If I understood correctly, I think your problem is much easily solved if you use a Map. As the key you store the word ("Word1", "Word2", etc...) and as the value the number of times. For example (Java 7):

final Map<String, Integer> wordsUsed = new HashMap<>();
// Put word
final String curUsed = wordsUsed.get(wordToPut);
if (curUsed == null) {  // New word, put counter to 1
    wordsUsed.put(wordToPut, 1);
} else { // Old word, increase previous counter
    wordsUsed.put(wordToPut, ++curUsed);
}

If you want to preserve insertion order, you can use a LinkedHashMap instead.

As a side note, arrays in Java are rarely the solution to your problem. JDK data structures provide almost always a much better and faster (code-writing speaking) solution.

2 Comments

Seeing as I probably won't be able to do it with arrays then I will give your option a go. The thing is I need to use these words in swing later in the program and compare the words to eachother. Is this still possible with maps and how would you do such a thing? I have little experience with maps. Thanks
Of course you can use Maps in Swing, why wouldn't? I don't know how if you won't explain what you want to do in Swing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.