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")); } }
number < 1000 && number >= 100. :| besides, you're doingnumberAsString. you could call length() on this to get the length of the string.number < 1000 && number >= 100. but i'd advise against it for a palindrome. why? what if the user enters010. for int, it'll be seen as 10 and hence give an error. but as string, it'll be a palidrome. it's up to you to decide which result you wanna show to user, and hence use that method.