I've written an application that determines whether an input (a 5-digit number) is a palindrome or not.
1. After I've finished the code, it's long and not readable. Would you give me hints of an alternate method?
2. After the user has inputted a 6 or more digit long
number, or a 4 or less digit long number, an error message will be displayed and the application will be terminated. Would you give me some hints to let the user enter a number again?
Thank you.
The code:
import javax.swing.JOptionPane;
public class Palindrome {
public static void main( String args[] )
{
String testNum;
int testVal;
testNum = JOptionPane.showInputDialog ( "Input a five-digit integer" );
testVal = Integer.parseInt( testNum );
if ( testVal >= 100000 )
JOptionPane.showMessageDialog(
null, "The input is more than five-digit long",
"Test Result", JOptionPane.ERROR_MESSAGE );
else
if ( testVal <10000 )
JOptionPane.showMessageDialog(
null, "The input is less than five-digit long",
"Test Result", JOptionPane.ERROR_MESSAGE );
else
if (
(
( testVal - testVal % 10000 )
/ 10000
)
==
( testVal % 10000 % 1000 % 100 % 10 )
&&
(
(
( testVal % 10000 ) - (testVal % 10000 % 1000 )
)
/ 1000
)
==
(
(
(testVal % 10000 % 1000 % 100 )
- ( testVal % 10000 % 1000 % 100 % 10 )
)
/ 10
)
)
JOptionPane.showMessageDialog(
null, "The input is a palindrome",
"Test Result", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(
null, "The input is not a palindrome",
"Test Result", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
1. After I've finished the code, it's long and not readable. Would you give me hints of an alternate method?
2. After the user has inputted a 6 or more digit long
number, or a 4 or less digit long number, an error message will be displayed and the application will be terminated. Would you give me some hints to let the user enter a number again?
Thank you.
The code:
import javax.swing.JOptionPane;
public class Palindrome {
public static void main( String args[] )
{
String testNum;
int testVal;
testNum = JOptionPane.showInputDialog ( "Input a five-digit integer" );
testVal = Integer.parseInt( testNum );
if ( testVal >= 100000 )
JOptionPane.showMessageDialog(
null, "The input is more than five-digit long",
"Test Result", JOptionPane.ERROR_MESSAGE );
else
if ( testVal <10000 )
JOptionPane.showMessageDialog(
null, "The input is less than five-digit long",
"Test Result", JOptionPane.ERROR_MESSAGE );
else
if (
(
( testVal - testVal % 10000 )
/ 10000
)
==
( testVal % 10000 % 1000 % 100 % 10 )
&&
(
(
( testVal % 10000 ) - (testVal % 10000 % 1000 )
)
/ 1000
)
==
(
(
(testVal % 10000 % 1000 % 100 )
- ( testVal % 10000 % 1000 % 100 % 10 )
)
/ 10
)
)
JOptionPane.showMessageDialog(
null, "The input is a palindrome",
"Test Result", JOptionPane.INFORMATION_MESSAGE );
else
JOptionPane.showMessageDialog(
null, "The input is not a palindrome",
"Test Result", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}