1

I have a program, that will find all the possible permutations of a word given as a command line argument, but am unable to get any output from the program, the program compiles fine, and when I have run through the program, i can't see what's wrong. Any ideas?

    import java.io.*;
02   
03  public class Anagrams
04  {
05      private static char [] word;
06      private static char [] permutation;
07      private static boolean [] characterUsed;
08   
09       
10       
11      public static void main(String [] args)throws Exception
12      {
13        
14         word = args[0].toCharArray();
15         permutation = new char[word.length];
16         characterUsed =  new boolean[word.length];
17         printPermutations(0);
18      }//main
19       
20     private static void printPermutations(int currentIndex)throws Exception          
02     {
03   
04      if(currentIndex == permutation.length)
05          System.out.println(permutation);
06      else
07      {
08          for(int index=0;index<word.length-1;index++)
09          {
10  //if the character at that index hasn't been used       
11             if(!characterUsed[index]);
12              {
13                 //mark character at this position as in use
14                 characterUsed[index] = true;
15                 //put the character in the permutation
16                permutation[index]= word[currentIndex];
17                 printPermutations(currentIndex +1);
18                 characterUsed[index] = false;
19               }//if
20           }//for
21         }//else
22       }//printPermutation
41  }//Anagrams
AkshaiShah
  • 5,739
  • 11
  • 37
  • 45
  • Have you tried stepping through your code with a debugger to actually see what happens, and whether that is the expected behavior. – Robin May 03 '12 at 16:41
  • Next time, try putting in some System outs to see how far you are getting down in your recursion. That's all it took to figure it out. – John B May 03 '12 at 16:48
  • I think your approach is not 100% accurate, look at this one http://stackoverflow.com/a/4240323/643500 – Sully May 03 '12 at 16:50

4 Answers4

2

change

permutations[index] = permutations[currentIndex];

to

permutations[index] = argument[currentIndex];

premutation has not been pre-populated so you are always assigning it to the null character.

In the future doing something like System.out.println("<"+myString+">"); is helpful for these kinds of issues.

And change

for (int index = 0; index < argument.length-1; index++)

to

for (int index = 0; index < argument.length; index++)

John B
  • 32,493
  • 6
  • 77
  • 98
  • Beat me to it =) I was having trouble keeping track of the recursion in my head so I had to take a min to do it out on paper, and that's when I saw you never used the values in `argument`. Haven't finished to see if the recursion is right, but you'll need to fix this. – Windle May 03 '12 at 16:42
2

Not sure if this is the only problem, but this line also looks iffy:

for (int index = 0; index < argument.length - 1; index++)

Are you meaning not to use the last char in the array? You probably mean:

for (int index = 0; index <= argument.length - 1; index++)

or

for (int index = 0; index < argument.length; index++)
Windle
  • 1,385
  • 2
  • 14
  • 33
1

The reason it is not printing anything is because of an error in the for-loop. Try

for (int index = 0; index < argument.length; index++)

user845279
  • 2,794
  • 1
  • 20
  • 38
  • Probably were typing this up at the same time so +1. Also, if you put 4 spaces at the start of lines of code it will format them properly for you – Windle May 03 '12 at 16:53
1

I believe problem is on line 11 & 12. Did you really intend to have the ; at the end of if condition?

10  //if the character at that index hasn't been used       
11             if(!characterUsed[index]);

Hope that helps..

Qadeer
  • 11
  • 1