The program is supposed to find all the combinations of the letters in "computer", the algorithm was given to me, but I don't have any idea of how it dos its work, all I get from this is a an infinite amount of "nullp"
Here is the code:
public class Anagrams {
public static void printAllAnagrams (String wordSoFar, char[] lettersRemaining) {
if (lettersRemaining.length==0)
System.out.println(wordSoFar);//found a word
else
for (int i=0; i<lettersRemaining.length; i++)
{
char []lettersRemaining2 = new char [lettersRemaining.length-1];
String wordSoFar2 = wordSoFar + lettersRemaining[i];
printAllAnagrams(wordSoFar2, lettersRemaining2);
}
}
public static void main(String[] args) {
String wordSoFar=null;
char[] lettersRemaining= {'c','o','m','p','u','t','e','r','s'};
boolean checkException = false;
do {
try {
printAllAnagrams (wordSoFar, lettersRemaining);
checkException = true;
}
catch (Exception x)
{
System.out.println("An error has occured.");
}
} while (checkException == false);//remains in loop as long as the boolean variable is false
}
}