[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2242
  • Last Modified:

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

0
jovanvliet
Asked:
jovanvliet
  • 5
  • 3
  • 2
  • +2
3 Solutions
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now