Hello,
I am a student attempting to complete an assignment for school. The assignment is:
Write an application called Palindrome.java that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message dialog indicating the problem to the user. When the user dismisses the error dialog, allow the user to enter a new value.
Your program will have the following four methods:
- main() method, which controls the execution of the program
- retrieveInput() method, which prompts and retrieves the input values
- check() method, which determines whether it is a palindrome
- display() method, which displays the result.
I have written the following code:
import javax.swing.JOptionPane;
public class palindrome {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//variables
double number;
boolean x;
boolean display;
//call methods
number = getUserInput();
x = check();
display = display();
}
//Request user input
private static int getUserInput() {
// TODO Auto-generated method stub
//declare method variables
int inputNumber = 0;
String answer = JOptionPane.showInputDialog(null, "Enter a five digit number");
inputNumber = Integer.parseInt(answer);
return 0;
}
private static boolean check(){
//Declaring variables
int number = getUserInput();
//Array statement
int[] myArray = new int[5];
//For..Loop to load numbers into Array.
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 is a Palindrome!!!",
"Correct!",
JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null,
"The number you entered is not a Palindrome!",
"Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
However, the code does not go past displaying a text box asking the user to enter a 5 digit # and it displays this box twice.
I would appreciate anyones helps.
Thanks.