I know that there are many other topics on this issue, and I have read many, many of them and none seem to help. I am new to Java and so I feel that maybe I am missing something simple. If I use "Clean and Build" no errors, but as soon as I attempt to run or debug the code, I am given a "No main class found."
If I change the "Public Void Main" and "Public Void IsPrime" to include "Static", then I am given "non-static variable factor cannot be referenced from a static context" errors.
Can anyone provide any guidance?
public class LargestPrime {
/**
*
*/
public long factor;
/**
* @param args the command line arguments
*/
public static void main(String[] args){
double var, sqvar;
var = 600851475143L;
sqvar = Math.sqrt(var);
ArrayList<Long> factors = new ArrayList<>();
long i = 1;
while(i <= sqvar){
if (var % i == 0){
if(i % 2 != 0 ){
factors.add(i);
}
}
i = i + 2;
}
ArrayList<Long> primes = new ArrayList<>();
int j;
for(j = 0; j <= factors.size(); j++){
factor = factors.get(j);
LargestPrime obj = new LargestPrime();
obj.isPrime(factor);
}
}
/**
*
* @param testing
*
*/
public static void isPrime(long testing){
testing = factor;
System.out.println(testing);
}
}
Try to change the factor
variable to a static
like this public static long factor;
variable.
And replace this for
loop:
for(j = 0; j <= factors.size(); j++){
With using <
instead of <=
because the size of ArrayList from 0
to size-1
:
for(j = 0; j < factors.size(); j++){