arithmetic exceptions when calling method
Trying to write a simple math program that finds the prime factorial of a number. here's the code:
package pack1;
import javax.swing.JOptionPane;
publicclass PrimeFactorial{
/**
1. have user enter a whole number
2. find lowest factor of the number; put it into an array of prime factors
3. divide input number by lowest factor
4. determine if new number is prime, if so we're done, if not repeat until
the product of the division is prime.
*/
publicstaticvoid main(String[] args){
String input=JOptionPane.showInputDialog("please enter a whole number: ");
int number = Integer.parseInt(input);
PrimeFactorial jw =new PrimeFactorial();
int[] primes =newint[99];
int i = 2;
int factorFlag = 0;
int newnum;
while (number % i != 0){
i++;
}
System.out.println("the value for i is " + i);
//place first prime factor into array
primes[factorFlag] = i;
newnum = (number / i);
System.out.println("the value of newnum is " + newnum +", and the first array value is: " + primes[factorFlag]);
//reset variables
i = 2;
factorFlag++;
//determine if newnum is prime
if (jw.primeChecker(newnum)==true){
System.out.println( newnum +" is a prime number");
}else{
System.out.println( newnum +" is a prime number");
}
}
publicboolean primeChecker(int newnum){
int primeFlag=0;
for(int t=0;t<newnum;t++){
if (newnum % t == 0){
primeFlag++;
}
}
if (primeFlag == 0){
returntrue;
}else
returnfalse;
}
}
I get this error:
the valuefor i is 2
the value of newnum is 12, and the first array value is: 2
java.lang.ArithmeticException: / by zero
at pack1.PrimeFactorial.primeChecker(PrimeFactorial.java:52)
at pack1.PrimeFactorial.main(PrimeFactorial.java:41)
I'm not sure if I created the object correctly.
thanks,
bp>

