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:

  1. main() method, which controls the execution of the program
  2. retrieveInput() method, which prompts and retrieves the input values
  3. check() method, which determines whether it is a palindrome
  4. 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.

Recommended Answers

All 7 Replies

It has been a while since I've used Java, but I think I see a few issues. Your method getUserInput() always returns 0. It should return inputNumber if Integer.parseInt(answer); successfully parse the integer, maybe 0 if the parse fails. Actually -1 would be even better because 00000 is equal to 0, but is still a palindrome.

You're seeing the box twice because you call it once in the main function with number = getUserInput(); and again in the check() function, also called from main().

Another note. Why convert the input to an integer? Why not leave it as a string and just check for numerical values?

I think that should get you a little farther. You do have to figure some of this out yourself, seeing as how it's for school and everything.

I've just made a quick php web app to do what you need. I did it all using strings and not integers or doubles. Of course this will likely not help you with your code, but at least it may serve as an example.

http://dcdworld.com/palindrome.php

Thanks for your comment and suggestions. I have made some changes to the code. However the code still displays the enter a five digit number twice but after the second entry it accurately tells whethter it is a palindrome or not.

Any more suggestions would be helpful.

import javax.swing.JOptionPane;


public class palindrome {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //variables

        boolean x;
        boolean display;

        //call methods

        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 inputNumber;

        }
        //Checks user input to verify whether or not it is a Palindrome
        private static boolean check(){

            //Declaring variables
            int inputNumber = getUserInput();
            int number = inputNumber;

            //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;

            }
            //displays the outcome
            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;


        }

        }

Thanks again.

Thanks for your help. I figured it out. I was calling the getUserInput() twice in the main, like you said, by this statement x = check();

My palindrome code works, but when a user enters a non palindrome number it needs to allow them to enter another number. I have added an if statement in the main class, but all this has done is run the application twice no matter if the entry is correct or not correct.

import javax.swing.JOptionPane;


public class palindrome {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //variables     
        boolean display;

        //call methods
        display = display();


        if (display != true){
            getUserInput();
        }
        else {
            System.exit(0);
        }


    }

    //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 inputNumber;

        }
        //Checks user input to verify whether or not it is a Palindrome
        private static boolean check(){

            //Declaring variables
            int inputNumber = getUserInput();
            int number = inputNumber;

            //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;

            }
            //displays the outcome
            public static boolean display(){

                //Variable
              //boolean inputNumber = getUserInput();

            if (check() == true) {
            JOptionPane.showMessageDialog(null, "The number your enter 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;


        }

        }

If anyone could provide some insight I would appreciate it.

Welcome Question???

You must have to read rule at daniweb,
1.Homework policy
2.How to post source code?
3.Title of thread must reflect your question.

Source code must be surrounded with code tags.
For example,

[CODE=Java] ... statements..

[/code]

I apologize, I should have check the posting rules. I will make sure to do so in the future. As I am sure you can see I am clearly not trying to have others do my homework. I am just looking for suggestions on the code I produced. If you or anyone else can help me with the last question I posed, I would appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.