java - How to check if a three digit number is a palindrome? -


this question has answer here:

i creating little trivial palindrome quiz in java.

i should @ point check if user enter more or less 3 digit number such "1011" or "10" , display error message such "wrong input." however, can't check length of number int.length() in java, instead string (string.length()).

how solve problem?

this code without if-three-digit check

import java.util.scanner; public class test {     public static void main(string[] args) {         system.out.println("please enter 3 digit number number , check if it's palindrome.");          // create scanner , 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) ? " palindrome" : " not palindrome"));       } } 

to check whether number has properties, best use numeric operations, if possible. leads faster , more straight-forward code.

the smallest number 3 digits 100. largest number 3 digits 999. can test follows:

if ((number < 100) && (number > 999)) {   system.out.println("not three-digit number :(") } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -