0

I am trying to create a population of chromosomes in which an individual chromosome consist of an array of char characters. Each character in the array is unique and is obtained from the user. Below is my main class;

public class Main {
   static String rawData;
   static char [] charArray;

   public static void main(String[]args) {
      Scanner user_input = new Scanner(System.in);
      System.out.print("Please enter your message here.\n");
      rawData = user_input.nextLine();
   }
}

and my individual chromosome(array) looks like this;

public class Individual extends Main {

   void CreateIndiv() {
      charArray = rawData.toCharArray();
      for (int i=0; i<charArray.length; i++) {
         for (int j=i+1; j<charArray.length; j++) {
            if (charArray[i] == charArray[j]) {
               charArray[i] = 0;
            }
         }
      }

      for (int i=0; i<charArray.length; i++) {
         if (charArray[i] != 0) { 
            System.out.print(charArray[i]);
         }
      }
   }
}

Could any one assist me to create random population (arrays) from this individual char array. Please correct me if I am using the wrong code to create an individual.

Steffen
  • 2,381
  • 4
  • 20
  • 33
Phil
  • 1
  • 1
  • 1
    Please first assist us and format your code as to make it more readable. – Thomas Mar 22 '16 at 09:34
  • 1
    As for your code: If you want to have more than one individual per run you should _not_ use static variables (it's better to not use them in most cases anyway). How to randomly populate an array (or shuffle it after creation) refer here: http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array (or use a list instead as well as `Collections.shuffle(list)`). – Thomas Mar 22 '16 at 09:36

0 Answers0