Need help giving thesame error over and over again
Hi all,
The code below is the code for getting the factorial of a number. The program takes the input from the user and checks if it is correct and does the factorial and gives a result according if it is over 20 or not. The problem is that i tried to run, it gives me the same error everytime.Could someone run it too and see what the problem is. By the way the error i was getting was:
init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Factorial.getInput(Factorial.java:30)
at Factorial.main(Factorial.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
import java.math.*;
import javax.swing.*;
publicclass Factorial{
publicstaticvoid main(String[] args){
int n;
try{
n = getInput(args);// can throw InvalidInput
int nfac;
try{
nfac = fac(n);// can throw Overflow
System.out.println(n +"! = " + nfac);
}
catch(Overflow e){
System.out.println("abnormal termination of fac()");
System.out.println("no result available");
}
}
catch(InvalidInput e){
System.out.println("abnormal termination of getInput()");
System.out.println("no result available");
}
}
publicstaticint getInput(String[] args)throws InvalidInput{
int n = Integer.parseInt(args[0]);
if (n < 0)
thrownew InvalidInput("negative input not allowed!");
System.out.println("normal termination of getInput()");
return(n);
}
publicstaticint fac(int k)throws Overflow{
int kk, kfac = 1;
for (kk = 1; kk <= k; kk++){
if (kfac > Integer.MAX_VALUE / (kk + 1))
thrownew Overflow("overflow!");
kfac *= kk;
}
System.out.println("normal termination of fac()");
return kfac;
}
}
class InvalidInputextends Exception{
InvalidInput(){}
InvalidInput(String s){System.out.println(s);}
}
class Overflowextends Exception{
Overflow(){}
Overflow(String s){System.out.println(s);}
}

