shuffling a character array in Java

Hi,

I am a beginning student in Java and would really appreciate any help you can give me.

As part of review program preparing for an exam, I am trying to randomize an array of letters of the alphabet. My code does randomize the letters but repeats some of them in the new array. Could you please tell me what I am doing wrong? I only want each letter to appear once in the randomLetters array. Many thanks! Jo

//--- Shuffle by exchanging each element randomly
    char randomLetters[]; //initialize array for shuffled letters
    randomLetters=new char[letters.length];//allocate memory space same length as original array of the alphabet array
     
    for (int i=0; i< letters.length; i++) { //actually copies each letter from letters[i] into randomLetters[i] instead of just pointing/referencing
      randomLetters[i]=letters[i];
    }
    for (int i=0; i<randomLetters.length; i++) {
      int randomLetter = randomGenerator.nextInt(randomLetters.length);
      char temp = randomLetters[randomLetter];// holds temp value of letter - used below
      randomLetters[randomLetter]= randomLetters[i];
      randomLetters[i]=temp;//this puts letter/number that was held back into scrambled array
      randomLetters[i] = randomLetters[randomLetter];
       
    }
//--- Shuffle by exchanging each element randomly 
    char randomLetters[]; //initialize array for shuffled letters 
    randomLetters=new char[letters.length];//allocate memory space same length as original array of the alphabet array 
     
    for (int i=0; i< letters.length; i++) { //actually copies each letter from letters[i] into randomLetters[i] instead of just pointing/referencing 
      randomLetters[i]=letters[i]; 
    } 
    for (int i=0; i<randomLetters.length; i++) { 
      int randomLetter = randomGenerator.nextInt(randomLetters.length); 
      char temp = randomLetters[randomLetter];// holds temp value of letter - used below 
      randomLetters[randomLetter]= randomLetters[i]; 
      randomLetters[i]=temp;//this puts letter/number that was held back into scrambled array 
      randomLetters[i] = randomLetters[randomLetter]; 
       
    }

Open in new window

jovanvlietAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Try using the collections.sort() method.
0
a_bCommented:
Sorry, I meant Collections.shuffle();
0
a_bCommented:
Have a look at the Knuth shuffle algorithm - http://tekpool.wordpress.com/2006/10/06/shuffling-shuffle-a-deck-of-cards-knuth-shuffle/

Try and incorporate that into  your code if you donot want to use in built java methods
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CPColinSenior Java ArchitectCommented:
Remove line 13. It's duplicating characters.
0
CEHJCommented:
You'd probably be better off with a Collection of single letter String. Then you can shuffle it more easrily
0
SunScreenCertCommented:
There are actually 100 ways of doing a program. But as a beginner you should try to get doing things in a better and optimized way in the first place itself, so in the long run you would get the habit of that.

First of all, why do you need a temp array.

And as suggested by others, do you want to use Collections or you want to try it first with array itself.

0
jovanvlietAuthor Commented:
CPColin,
Thank you for letting me know I had a redundant line of code!

a_b
Thank you for the Knuth info and code.
I tried it and got a DrJava error message:
"cannot find symbol      method randomGenerator(int,int)"
I very ignorant. Do I need to create a method or import something?
I have put the revised code below in case you have time to look at it.
Thanks!
Jo


[code]
    import java.util.Random;  
    Random randomGenerator = new Random();
    
    int rand;
    
    for (int i=26; i>=0; i--) {
      int min=0;
      int max=i;
      rand=randomGenerator(0,i);
      swap(letters[i], letters[rand]);
      
      System.out.println(letters ); 
      
    }

Open in new window

0
SunScreenCertCommented:
You should try a few sort algorithms and you would come to know how each one behaves and start using the best one.
0
CEHJCommented:
>>rand=randomGenerator(0,i);

should be
rand=randomGenerator.nextInt(26);

Open in new window

0
jovanvlietAuthor Commented:
Oh! Thanks you. you're very patient!
May I ask one more question???
I now get an error
cannot find symbol  method swap(char, char)
for this line of code
swap(letters[i], letters[rand]);

I was trying to follow the
swap(pArr[i], pArr[rand]);  
in the Knuth example

How did I mess this up?
Thanks,
Jo
(We study Collections and Lists in a few weeks but haven't gotten there yet so we ar to use arrays)
0
CEHJCommented:
You need to copy the swap method from that code into yours
0
CEHJCommented:
Actually you'd be better off implementing swap like this:
private void swap (char[] array, int index1, int index2) {
	char temp = array[index1];
	array[index1] = array[index2];
	array[index2] = temp;
}

Open in new window

0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.