I need to find the numbers in a series that meet criteria set up in two different methods.
I have tried moving the statement that appends the variable primepalindromes out of the for loop in the main method but that gives me a number a single number past the series of number I set to be checked.
public static void main(String[] args) {
boolean isPrime = true;
boolean isPalindrome = true;
String primepalindromes = "";
int j;
for (j = 1; j <= 100; j++) {
checkprime(isPrime, j);
if (isPrime = true) {
checkpalindrome(isPalindrome, j);
if (isPalindrome = true) {
primepalindromes = primepalindromes + j + " ";
}
}
}
System.out.println(primepalindromes);
}
private static boolean checkprime(boolean isPrime, int j) {
int temp = 0;
for (int i = 2; i <= j / 2; i++) {
temp = j % i;
if (temp == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
private static boolean checkpalindrome(boolean isPalindrome, int j) {
int r, sum = 0, temp;
temp = j;
while (j > 0) {
r = j % 10;
sum = (sum * 10) + r;
j = j / 10;
}
if (temp == sum) {
isPalindrome = false;
}
return isPalindrome;
}
The code is supposed to return all numbers in the set series that fit the criteria of the two methods but instead it just gives all of the numbers in that series.
checkpalindromereturns a value which is being ignored. It's not clear what you're trying to achieve...