0

I am trying to run a java program to find if number is palindrome or not but at the time of compilation i am getting error message as - Exception in thread "main" java.lang.Error: Unresolved compilation problem: This method must return result of type Boolean.

below is my program code :

package testing;
import java.util.Scanner;
public class PalindromeNumber { 
    public static void main(String[] args) {
        System.out.println("Enter a number to check if it is palindrome or not :");
        int number1 = new Scanner (System.in).nextInt();
        if(PalindromeCheck(number1)) {
            System.out.println("Number " +number1+ " is palindrome.");
        }
        else {
            System.out.println("Number " + number1 + " is not palindrome.");
        }
    }




public static boolean PalindromeCheck(int number) {
    int palindrome = number;
    int reverse = 0;
    while (palindrome !=0) {
        int remainder = palindrome % 10;
        reverse = reverse * 10 + remainder;
        palindrome = palindrome / 10;
        if (number == reverse) {
            return true;
        }

    return false;
}
}
}
5
  • 3
    Put one of the 3 ending curlybraces before the Line 'return false;' Commented Jun 11, 2016 at 4:54
  • Thanks @Pras. It worked. can you please tell me what my mistake was ? Commented Jun 11, 2016 at 4:56
  • 1
    From what I see it doesn't look like you're working in an IDE. While I can't force you to, I do recommend you switch to one. An IDE can resolve and identify problems for you, as well as provide suggestions to help you code faster. Commented Jun 11, 2016 at 5:00
  • Your mistake was failing to include a required ending brace in the correct place, followed by ignoring a compiler error message and attempting to execute the resulting invalid class. As @WinterRoberts says, use an IDE, Eclipse or NetBeans if you want a free one. Commented Jun 11, 2016 at 5:00
  • The curly brace i asked u to move is the "end" of while loop : you were returning false in the loop actually Commented Jun 11, 2016 at 5:00

2 Answers 2

1

What if the number=0 at the begining? Your code won't return anything at that moment. Return false, outside the while loop.

public static boolean PalindromeCheck(int number) {
    int palindrome = number;
    int reverse = 0;
    while (palindrome !=0) {
        int remainder = palindrome % 10;
        reverse = reverse * 10 + remainder;
        palindrome = palindrome / 10;
        if (number == reverse) {
            return true;
        }
    }
    return false; //Note the curly brace above. Now the method will always return either true or false
}
Sign up to request clarification or add additional context in comments.

Comments

1
public static boolean PalindromeCheck(int number) {
    int palindrome = number;
    int reverse = 0;
    while (palindrome !=0) {
        int remainder = palindrome % 10;
        reverse = reverse * 10 + remainder;
        palindrome = palindrome / 10;
        if (number == reverse) {
            return true;
        }

    return false;
}
}
}

In this case, while loop also includes return false. So, in the case when while loop is completed , you don't have a return value. So, the following will work:

public static boolean PalindromeCheck(int number) {
    int palindrome = number;
    int reverse = 0;
    while (palindrome !=0) {
        int remainder = palindrome % 10;
        reverse = reverse * 10 + remainder;
        palindrome = palindrome / 10;
        if (number == reverse) {
            return true;
        }
    }

    return false;

}
}

2 Comments

Thanks @Priyansh Goel
@AkshayJadhav : Please accept the answer <Click on tick to accept>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.