Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Help generating random permutations from array list

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help generating random permutations from array list

    There is no output for this code. I don't know why. It seems like it should work. Can someone please let me know what they think i need to change. Here is the problem.

    https://students.ga.desire2learn.com...eeResponse.pdf

     
     
     
    import java.util.Random; //imports the Random java class
    public class PermutationGenerator
     
     
      {
      /*This method iterates the nextPermutation() method 10 times
       *to produce 10 differernt arrays of size 10 with integers
       *1 to 10 inclusive*/
     
      public void PGenerator()
      {
     
      PermutationGenerator a = new PermutationGenerator();
      for (int i=0;i<10;i++) a.nextPermutation();//This loop repeats the nextPermutation method 10 times to produce 10 different arrays
      }
     
      /*This method returns an array of 10 integers from 
       *1 to 10 inclusive in a random order by creating a 
       *new array from permutations of a base array*/
      public int[] nextPermutation()
      {
        Random rand = new Random();//Creates the random generator
        int baseSize = 0;//Initializes the base counter as 0
        int[] base = new int[10];//Initializes the base array
        for (int i = 0; i<10; i++){ //This loop fills up the base array with integers 1 to 10 inclusive
          base[i] = i + 1;
          baseSize++;//Increases the counter for base size
          System.out.println (base);
        }
        int[] mutation = new int[10];//Initializes the mutation array
        for (int i = 0; i<10; i++){
          int a = rand.nextInt(baseSize); //Picks a random index between 0 and the length of the base array
          mutation[i] = base[a]; //Sets the ith index of the result mutation array to the value at a in the base
          for (int j = a; j<baseSize - 1; j++){
            base[j] = base[j + 1];//This loop deletes the value at index a in the base array to prevent it from being chosen again
          System.out.println (base);
          }
          baseSize--;//Decreases the length of the base array
        }
        return mutation;
      }
     
     
     
    public static void main(String[] args)
    {
    }
    }
     
     
    public static void main(String[] args)
    {
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    There is no output for this code
    Add some println statements that print out messages as the code is executed and shows the execution flow and the values of variables as they are changed and used.
    Put the println statements in all the methods so you know what is being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help generating random permutations from array list

    i just tried that and not even the first part is being executed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    What methods did you see were being executed? What was printed out?
    Did the method that was executed call any other methods?
    Where those other methods executed?
    You'll know when a method is executed when the println statement prints out a message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help generating random permutations from array list

    Nothing is executed. It still doesn't print anything for any step.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help generating random permutations from array list

    So take Norm's advice, and locate the first method to be called, and see what it does. Surely at least one method gets called somewhere, sometime, right? Start there.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    Are there println methods in all the methods? The one in the main() method should print.
    If the main() method doesn't execute, then you must get an error message.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help generating random permutations from array list

    I placed a println method at each step and nothing is printed. There is no error message.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    The one in main() has to print. Can you post the new code that shows where the printlns are located?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help generating random permutations from array list

    I edited the original post to reflect changes.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    Where are the println statements?
    Please post the code with the println statements that you were executing that did not print out anything when it was executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help generating random permutations from array list

    they are in the code. maybe i did not put them in the right spots? there are two.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help generating random permutations from array list

    there are two.
    There should be one in each method at the first thing in the method. You want to see where the code is executing.
    There should be lots of them. 2 is not enough.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: November 29th, 2012, 01:25 PM
  2. Generating array (or arraylist) matrix from txt file
    By benjalizana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 14th, 2011, 08:09 PM
  3. Generating a Random Coprime Number
    By phleep in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 15th, 2011, 12:12 AM
  4. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  5. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM