-1

I have a question to input a 5 digit integer. The program will output if it is a palindrome or not. And it will display "not a palindrome" if integer is not a palindrome.

But the problem is that the program crash whenever i input a less than 5 digit integer. Else everything is fine. The program even output "not a 5 digit number" when i input an integer with more than 5 digit.

Here is my code.

String input1;
int number, number2, a, b, d, e;

input1 = JOptionPane.showInputDialog("Enter 5 digit number");

a = input1.charAt(0);
b = input1.charAt(1);
d = input1.charAt(3);
e = input1.charAt(4);

number2 = input1.length();
number = Integer.parseInt(input1);

if (number2 >= 6) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
}
if (number2 <= 4) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
}

if ((a == e) && (b == d)) {
    JOptionPane.showMessageDialog(null, "Palindrome");
} else {
    JOptionPane.showMessageDialog(null, "Not a palindrome");
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 6
    what do you think happens when `input1` is less than 5 and you do `e = input1.charAt(4);` – depperm Sep 27 '18 at 14:46
  • 2
    Forget about `a, b, c, d, e`. Use `if (input1.charAt(0) == input1.charAt(number-1-0)` with a counter/index you can then have a general solution for all lengths. – Joop Eggen Sep 27 '18 at 14:48

5 Answers5

0

number2 <= 4 will not Filter out 4 digit numbers. Also, proof the lenght before you e = input1.charAt(4);

elPolloLoco
  • 291
  • 1
  • 8
0

You just need to re-arrange your code. input1.charAt(4) could fail if input1.length() < 5

input1 = JOptionPane.showInputDialog("Enter 5 digit number");   

number2 = input1.length();
number = Integer.parseInt(input1);

if (number2 >= 6) { 
    JOptionPane.showMessageDialog(null, "Not a 5 digit number"); 
    return;
}
if (number2 <= 4) { 
    JOptionPane.showMessageDialog(null, "Not a 5 digit number"); 
    return;
}

a = input1.charAt(0);
b = input1.charAt(1);
d = input1.charAt(3);
e = input1.charAt(4);

if ((a == e) && (b == d)) { JOptionPane.showMessageDialog(null, "Palindrome"); }
else { JOptionPane.showMessageDialog(null, "Not a palindrome"); }
Isuru
  • 700
  • 9
  • 22
0

So the reason your program crashes is because you are getting the char at position x which may or may not exist. So if your length of the input is smaller than x , your code will throw an exception.

You can solve this by Adding a check before a = input1.charAt(0); like

 if (input1.length() <5){  
         JOptionPane.showMessageDialog(null, "Not a 5 digit number");  
 }  
Ashwin Valento
  • 878
  • 1
  • 7
  • 14
0

But the problem is that the program crash whenever i input a less than 5 digit integer.

It crashes because you can not access array element with index > array.length - 1 , for example if your array length is less than 5 i.e 4 then, your index should go from index=0 to index=3 however in your case , when you input a 4 length number your index should range from 0 to 3 . But the following will fail, because you are trying to access with index = 4 which is out of the range index of the array.

e = input1.charAt(4); //


Edit, instead you can simplify it as the following

String input1;
int number2,;

input1 = JOptionPane.showInputDialog("Enter 5 digit number");
number2 = input1.length();
if(number2 == 5) {
    if(input1.substring(0,2).equals(input1.substring(4,5)+input1.substring(3,4)))
        JOptionPane.showMessageDialog(null, "Palindrome");
    JOptionPane.showMessageDialog(null, "Not a palindrome");
}
JOptionPane.showMessageDialog(null, "Not a 5 digit number");   
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
0

Your problem was stated by many people but I will try to improve your code block in general.

Your issue is the block:

a = input1.charAt(0);
b = input1.charAt(1);
d = input1.charAt(3);
e = input1.charAt(4);

Lets say your input is "1234" the following array will exist

array["1","2","3","4] Arrays in java and most languages begin at 0. So your available positions are 0, 1, 2 and 3. When input1.charAt(4); runs it fails.


Extra Sugestions


But lets try to make all of your code slightly better.

First, assuming this is all of your code the line

number = Integer.parseInt(input1);

is useless as your never need this value, let's throw it out. If you keep it you will need a try...catch block in case input1 is not a number, "abcde" will crash your application here.

Next the block where you check for the length can be simplifed and should come before you try to read anything.

if (number2 >= 6) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
}
if (number2 <= 4) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
}

can just be made into a single if with the || (Or) statement or even better then != (NOT) statement, instead of checking if its less than four or more than 6, check if its Not 5.

if (number2 != 5) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
}

And you have some better code that shouldn't fail!

String input1;
int number2, a, b, d, e;

input1 = JOptionPane.showInputDialog("Enter 5 digit number");
number2 = input1.length();

if (number2 != 5) {
    JOptionPane.showMessageDialog(null, "Not a 5 digit number");
    return; //Dont do anything else, or you will get errors.
}

//There are better ways to do this, but this should work.
a = input1.charAt(0);
b = input1.charAt(1);
d = input1.charAt(3);
e = input1.charAt(4);

if ((a == e) && (b == d)) {
    JOptionPane.showMessageDialog(null, "Palindrome");
} else {
    JOptionPane.showMessageDialog(null, "Not a palindrome");
}

Sorry for the verbosity but I hope it helped.