I have to create a code with two methods that ask the user for a number and the program should tell whether it is a palidrome or not.
My code is the following:
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value: ");
int value = scan.nextInt();
int inversedNumber = reverse(value);
System.out.println("Is value " +value+ " a palindrome?: " +isPalidrome(value));
}
public static int reverse(int number)
{
int reverse = 0;
while( number != 0 )
{
reverse = reverse * 10;
reverse = reverse + number%10;
number = number/10;
}
return reverse;
}
public static boolean isPalidrome(int number)
{
boolean palidrome;
if(inversedNumber == number)
palidrome = true;
else
palidrome = false;
return palidrome;
}
}
But everytime I run it, I keep getting an error that tells me that inversedNumber
in the method isPalidrome
cannot be found. But it has been initialized in the main method. Should it not look for the initialization in the main method? Or my formating and/or logic are faulty.
inversedNumber
is declared in main()
and is not recognized in method isPalidrome()
because it's not in the same scope.
You can pass it to the method as follows:
call: isPalidrome(value, inversedNumber)
and change the method's signature to:
public static boolean isPalidrome(int number, int inversedNumber)