1

I'm hoping someone can answer this so I'll try to explain this well.

My goal is to generate a MAXIMUM of 3 unique vowels (AEIOU) on a line of 5 squares. I have made 25 squares using a 2D array (board[][]), but I want to do the first line first. Picture it like this: enter image description here

Now, my problem is, whenever I try to generate random letters in my squares, the first letter doesn't show. For example I have E and O, O would only show in my squares, and not E. It's printing in my console, but not in my GUI.

Also, sometimes DUPLICATES of letters are showing. I don't know how to fix this :|

Here are the codes I've done so far:

String board[][] = new String[5][5];
String alphabet = "AEIOU";
int numArray[] = new int[5]; //where I can store random indices of alphabet
int finalIndex = 0;

int random = (int) (Math.random()*3) + 1; //random number of vowels to be generated

//this loop covers everything
for(int ctr = 0; ctr < random; ctr++) {
  while(ctr != finalIndex) { //checks if there are any duplicates
    int rand = (int) (Math.random()*4); //random position for the letter
    numArray[ctr] = rand;
    while(numArray[ctr] != numArray[finalIndex]) {
      finalIndex++;
    }
  }

//finds the position of the letter in alphabet and converts it to String
  char character = alphabet.charAt(numArray[ctr]);
  String s = String.valueOf(character);
  System.out.println(s);

//loop for putting the letters to the 2D array
  for(int i = 0; i < board.length; i++) {
    int gen = (int) (Math.random()*4); //random square for letter
    for(int j = 0; j <= gen; j++) {
        if(i == 0 && j < 5) { //row 1
          board[i][gen] = s;
        }
    }
  }
}

I decided not to put my GUI code anymore just to make things simpler.

alicedimarco
  • 325
  • 1
  • 5
  • 20
  • I think I may know what's wrong but I don't know how to implement it. I think what's happening is that 2 letters have the same position, not allowing the other one to get a square. – alicedimarco Feb 25 '12 at 17:28
  • May you post one compilable code, I can test that for you !! – nIcE cOw Feb 25 '12 at 17:46
  • @GagandeepBali Hello! This is a compilable code, actually. Isn't it working for you? – alicedimarco Feb 26 '12 at 09:15
  • @GagandeepBali I'm not sure if it's working but I intended to put that while loop there in hopes of removing duplicates of my letters. That whole while loop 'filters' duplicates, not allowing it to enter numArray. – alicedimarco Feb 26 '12 at 09:32
  • 2
    As a side note I would like to suggest using the [java.util.Random](http://docs.oracle.com/javase/6/docs/api/java/util/Random.html) class to generate random integers instead of the Math.random() method multiplied and then rounded. Additionally, in order to select random elements from a pre-defined set of elements without getting duplicates, add them to a List, use [Collections.shuffle(list)](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)), and then just take out the first n elements from the list. – Jiddo Feb 26 '12 at 10:48

1 Answers1

1

Sorry I couldn't read what you had, so i tried this myself...

    int rows = 5;
    Character [] vowels = {'A','E','I','O','U'};
    Character [][] board = new Character [vowels.length][rows];

    for(int row = 0;row<rows;row++){
        ArrayList<Character> tempVowels = new ArrayList<Character>(Arrays.asList(vowels));
        int numVowPerLine = (int)Math.floor(Math.random()*4);
        for(int j = 0;j<numVowPerLine;j++){
            do{
                int pos = (int)Math.floor(Math.random()*5);
                if(board[row][pos] == null){
                    int temp = (int)Math.floor(Math.random()*tempVowels.size());
                    board[row][pos] = tempVowels.get(temp);
                    tempVowels.remove(temp);
                    break;
                }   
            }while(true);
        }
    }
L7ColWinters
  • 1,342
  • 1
  • 14
  • 31
  • I tried to compile your code, but some letters still have duplicates D: – alicedimarco Feb 26 '12 at 09:24
  • The code that L7ColWinters suggested will not create duplicates of any letters (within a row). If you still get duplicates displayed in your GUI then the problem is likely somewhere in your GUI code. I recommend that you readd the relevant parts of your GUI code to your question. – Jiddo Feb 26 '12 at 10:41
  • @Jiddo Thank you for enlightening me. I'll try this out with my GUI code as I have only tried it in console. – alicedimarco Feb 26 '12 at 17:00