I am creating a little trivial palindrome quiz in Java.
I should at some point check if the user will enter more or less than a three digit number such as "1011" or "10" and display an error message such as "Wrong input." However, I can't check the length of a number with int.length()
in Java, as I instead could do with a String (String.length()
).
How could I solve this problem?
This is my code without the if-three-digit check
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("Please enter a three digit number number and I will check if it's a palindrome.");
// create scanner and input number
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int digit1 = (int)(number / 100);
int remaining = number % 100;
int digit3 = (int)(remaining % 10);
System.out.println(number + ((digit1 == digit3) ? " is a palindrome" : " is not a palindrome"));
}
}