Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shorten the script

Status
Not open for further replies.

baum

Programmer
Aug 9, 2002
7
0
0
HK
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, &quot;The input is less than five-digit long&quot;,
&quot;Test Result&quot;, 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, &quot;The input is a palindrome&quot;,
&quot;Test Result&quot;, JOptionPane.INFORMATION_MESSAGE );

else
JOptionPane.showMessageDialog(
null, &quot;The input is not a palindrome&quot;,
&quot;Test Result&quot;, JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}

}
 
Hi,

Instead of writing everything in main just have a method and test all your conditions and check for the user's input.
If the input is not as specified,display the appropriate message and call the same function again,recursively.

VGG


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top