Hello! Tell me by code, what can I redo or add?

The task:

Create a program that will check whether a five-letter word entered by a user is a palindrome (examples: “lump”, “rotor”). If a non-5 letter word is entered, then report an error. The program should normally process the word, even if it uses different case characters. For example, the words “Lump” or “ROTOR” should also be considered palindromes.

public class Test { public static void main(String args []){ Scanner sr = new Scanner(System.in); String s; System.out.print("Введите слово из 5 букв -> "); if(sr.hasNext()){ s = sr.next(); if(s.length()==5){ s.toLowerCase(); s.toUpperCase(); System.out.print(s); }else{System.out.println("!!!Ошибка_Ввода!!!");}} } } 

Another such question: how to write the code, so that it is valid when entering a whole or a real number produced an error, since only string input should be used?

  • And where do you actually check for a palindrome? - Nofate

1 answer 1

I think this decision will come down:

 public static boolean isPolindrom(String s) { if (s.charAt(0) == s.charAt(4) && s.charAt(1) == s.charAt(3)) return true; return false; } public static void main(String[] args) throws Exception { Scanner sr = new Scanner(System.in); String s; boolean isString = true; System.out.print("Введите слово из 5 букв -> "); if (sr.hasNext()) { // Для Windows кодировка введёных данных cp1251 // Если Unix, то скорее всего будет koi8-r s = new String(sr.next().getBytes("cp1251")); try { // Проверяем введено ли число Double.parseDouble(s); isString = false; } catch (NumberFormatException e) {} if (s.length() != 5) { System.out.println("!!!Ошибка_Ввода!!! Не 5 букв"); } else if(!isString) { System.out.println("!!!Ошибка_Ввода!!! Введено число"); } else { if (isPolindrom(s)) System.out.println(s + " - палиндром"); else System.out.println(s + " - не палиндром"); } } } 

  • Thanks, @dude, it will go like this. - turtles