The following code works in Python but seems to return zero in Java for some reason. Can you point out any errors?
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
boolean run = true;
String s = null;
while (run){
s = JOptionPane.showInputDialog(null, "Enter a string [quit to exit]: ");
if (s.equalsIgnoreCase("quit")){
run = false;
}
try{
JOptionPane.showMessageDialog(null, "The string you entered can make " + permute(s) + " more words", "SUCCESS!", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception e){
System.out.println("possible division by zero happened");
}
}
}
/*Newbie function for finding out the occurrence of a char in a string
works good for the task at hand */
public static int count(char CHAR, String s){
int counter = 0;
for (int i = 0 ; i < s.length() ; i++){
if (s.charAt(i) == CHAR )
counter++;
}
return counter;
}
//The actual permute function, takes a string
public static final long permute(String s) throws Exception {
ArrayList<Integer> pqr = new ArrayList<>();
ArrayList<Character> checkList = new ArrayList<>();
s = s.toLowerCase();
int n = s.length();
int c = 0;
char character = 0;
for (int i = 0 ; i < n ; i++){
character = s.charAt(i);
c = count( character, s );
if (c>1 && !checkList.contains(character)){
pqr.add(c);
checkList.add(character);
}
}
long r = 0;
for (int j : pqr)
r *= fact(j);
return fact(n) / r;
}
//For finding out factorials
public static long fact(int number){
if (number <= 1){
return 1;
} else {
return number * fact(number - 1);
}
}
}