I have a code here that initially I would enter a five digit number. If the number is not equal to five an error box pops up. When I plug in a number that is not equal to five it brings up the original box to enter the number, it doesn’t give me the error box. Now when I go and plug a number again that is not equal to 5 then it brings up the error box. So it’s only after the second wrong attempt is when it works correctly.
I’m new to java coding so I’m sure it’s something simple. Thanks
public class Palindrome { public static void main(String[] args) { display(); } private static int getUserInput() { int inputNumber = 0; String answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number"); inputNumber = Integer.parseInt(answer); while(true) { answer = JOptionPane.showInputDialog(null, "Please enter a five digit number","Enter a number"); if(answer.length() == 5 ) return Integer.parseInt(answer); JOptionPane.showMessageDialog(null,"Please enter a 5 digit number!","Try again", JOptionPane.PLAIN_MESSAGE); } } private static boolean check(){ int inputNumber = getUserInput(); int number = inputNumber; int[] myArray = new int[5]; for (int i = 0; i < myArray.length; i++) { myArray[i] = (int) (number /(Math.pow(10,i)) % 10); } if(myArray[0] == myArray[4] && myArray[1] == myArray[3]) return true; else return false; } public static boolean display(){ if (check() == true) { JOptionPane.showMessageDialog(null, "This number is a Palindrome", "Excellent!", JOptionPane.INFORMATION_MESSAGE); } else JOptionPane.showMessageDialog(null, "Number is not a Palindrome!", "Sorry", JOptionPane.ERROR_MESSAGE); return false; } }