1

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.

1
  • checkpalindrome returns a value which is being ignored. It's not clear what you're trying to achieve... Commented Jun 27, 2019 at 0:49

1 Answer 1

3

The problem is in your 'if' statements :

if (isPrime = true) {

if (isPalindrome = true) {

The single "equals" sign is an assignment, and the value of that is the valuie being assigned - which in these cases is always true.

Change them to use double-equals operator :

if (isPrime == true) {

or, since these are boolean variable, it is better to simply use them directly :

if (isPrime) {

EDIT TO ADD: Also, you are not assigning the result of calling the functions. In a function, Java does not change the value of primitive types passed in as arguments, which means when you have :

boolean checkprime(boolean isPrime, int j)

and call it with, say, :

checkprime(someIsPrimeVariable, j);

Assigning a value to isPrime does not change the value of the variable that the caller supplied (ie someIsPrimeVariable is not changed).

So discard the isPrime argument, and instead just use the return value, so :

isPrime = checkprime(int j);

So your code would become :

for (j = 1; j <= 100; j++) {
    isPrime = checkprime(j);
    if (isPrime) {
        isPalindrome = checkpalindrome(j);
        if (isPalindrome) {
            primepalindromes = primepalindromes + j + " ";
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.