I was browsing the web for examples on how to partition in Java. I've got to this website that has a detailed example about Partitioning so I've tried to run it on my JCreator Pro.
I've only got one error, but I've tried all I can do to fix it ( Put BufferedReader, import java.io.*, throws IOException ), and It still wouldn't work.
Here's the Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Partition.main(Partition.java:17)
Here's the Code:
public class Partition {
public static void partition(int n) {
partition(n, n, "");
}
public static void partition(int n, int max, String prefix) {
if (n == 0) {
System.out.println(prefix);
return;
}
for (int i = Math.min(max, n); i >= 1; i--) {
partition(n-i, i, prefix + " " + i);
}
}
public static void main(String args[]) {
int N=Integer.parseInt(args[0]);
partition(N);
}
}
Thank you very much!