0

I am not sure why I am getting this problem. But basically I am shuffling a word and I am using an arrayList with loops.

This is my code and for debugging purposes I added a couple of lines.

//Declare and Initialize an arrayList for character checking
      ArrayList<Boolean> jumbleVerfiy = new ArrayList<Boolean>();
      for(int x = 0; x < chosenWord.length(); x++)
          jumbleVerfiy.add(false);

      while((chosenWord.length()) != (jumbledWord.length()))
      {
          //Generate random number for getting the random character
          int verficationNumber = rnd.nextInt(chosenWord.length());

          //Checks if the random number generated has been used before
          if(jumbleVerfiy.get(verficationNumber) == false)
          {
              //Get the character from the randomly generated number and change the boolean array to keep track of
              //letters used
              jumbledWord += chosenWord.charAt(verficationNumber);
              System.out.print(jumbleVerfiy.get(verficationNumber) + " " + verficationNumber);//debug
              jumbleVerfiy.add(verficationNumber, true);
              System.out.println(" " + jumbledWord + " " + jumbleVerfiy.get(verficationNumber));//debug
          }

My output is as follows

false 3 r true
false 4 ry true
false 2 ryr true
false 1 ryra true
false 4 ryray true
tarry ryray

In this output 4 was changed to true on the second run but then when it loops it is set to false for some reason. So I keep getting a duplicate letter. I am not sure what the problem is. Would love some insight.

Thank you,

5 Answers 5

2

You are adding values to the list, not setting them:

jumbleVerfiy.add(verficationNumber, true);

This means that if you for example first choose the second letter and then the second, the list ends up looking like (T,F,T,..) as if you had chosen the first and the third.

You need to use:

jumbleVerfiy.set(verficationNumber, true);
Sign up to request clarification or add additional context in comments.

Comments

1

In your loop you are mistakenly adding a new value to the array when you should be setting a value. The offending line:

jumbleVerfiy.add(verficationNumber, true);

Which should be calling set(index, value) instead. This should resolve your problem. You also might think of using a array instead of an ArrayList to avoid the overhead associated with the ArrayList since you don't need to expand once it's been initialized.

That would be:

boolean[] jumbleVerify = new boolean[chosenWord.length()];

Comments

0

In your code you need to use

jumbleVerfiy.set(verficationNumber, true);

other it will just get added to the arrayList

1 Comment

No, it won't get added to the end - it will be inserted at the specified location.
0

Because you setted new item to arraylist with index( add(index, value) ). If you set item ex. index 3, then index 3 old item went to be index 4.

Comments

0

I think your problem is that you are using:

jumbleVerfiy.add(verficationNumber, true);

which adds value on position. Use following instead and should work:

jumbleVerfiy.set(verficationNumber, true);

Comments

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.