简体   繁体   中英

Palindrome program not giving correct output

OK, I know I am missing something easy here, however I cannot find my mistake. The program runs fine, it just returns the wrong message. I am not calling it properly or something. I can remove the code form the display() method and put it in the check() method and it runs perfectly. Anyone want to help out a newbie who is stuck? I was unsure if the whole program needed to be displayed, if not I am sorry. A few times before I was told I did not put enough code in. I would also like any constructive feedback on the way I have written the code, as I do not want to pick up any bad habits.

public class Palindrome {

    // Main Method
    public static void main(String[] args) {

        // Declare variables
        int number;

        // Call method
        number = retrieveInput();
        check(number);
        display();


    }// End main method
    //*************************************************************************************
    // Method to retrieve the input
    public static int retrieveInput(){

        //Declare variables
        String number = null;
        int numInput = 0;
        boolean done = false;

        // Prompt user for input and validate that input
        while(!done){
            try{
                number = JOptionPane.showInputDialog("Enter 5 single digits");
                numInput = Integer.parseInt(number);
                if (numInput <10000 || numInput > 99999) 
            throw new NumberFormatException();
                else 
                    done = true;
            }

            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
            }

            }

        return numInput;

    }// End retrieveInput method
    //*************************************************************************************
    // Method to determine if input is a palindrome or not
    public static boolean check(int number){

        //Declare variables
        int num1;
        int num2;
        int num4;
        int num5;
        boolean isPalindrome = false;

        num1 = number / 10000;
        num2 = number % 10000 / 1000;
        num4 = number % 100/10;
        num5 = number % 10;

        // Checking to see if input is a palindrome
        if (num1 == num5 && num2 == num4);
        {
        isPalindrome = true;
        }

        if (num1 != num5 && num2 != num4);
        {
        isPalindrome = false;
        }


        return isPalindrome;

    }// End check method
    //*************************************************************************************
    // Method to display results
    public static void display(){

        // Declare variable
        boolean isPalindrome = false;


        // Display results
        if (isPalindrome == true)
            {
            JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
            }

            else 
            {
            JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
            }


    } // End display method 
        //************************************************************************************* 
} // End class Palindrome

Change the last two lines of main to read

boolean isPalindrome = check(number);
display(isPalindrome);

Change the declaration of display to

public static void display(boolean isPalindrome){

and remove the line in display that says

boolean isPalindrome = false;

That way, the isPalindrome that display works with will be the same one that was evaluated in check .

Edit:

Also remove the semicolon at the end of

if (num1 != num5 && num2 != num4);

otherwise the block underneath it will run in every case, and set isPalindrome back to false. Alternatively, you could just remove this condition and the block below it entirely, because it actually doesn't do anything.

isPalindrome should be in your main method and take the value from your check method. Then pass it to display method. The way it is now the isPalindrome is always false, because it is just initialized to false in your display method and never changes.

Oh my god, you did something very silly in the check method. You say that isPalindrome is false, then display the value of isPalindrome without taking into account the fact that isPalindrome is calculated by a very different method and you have no way to get the value of it. What you can do is the same approach you used to get the number out of retrieveInput and put in check : function arguments. Here's a revised display method:

public static void display(boolean isPalindrome){

    // Declare variable
    boolean isPalindrome = false;


    // Display results
    if (isPalindrome == true)
        {
        JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }

        else 
        {
        JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }


}

Then, the main method needs to look like this:

public static void main(String[] args) {

    // Declare variables
    int number;
    boolean isPalindrome

    // Call method
    number = retrieveInput();
    isPalindrome = check(number);
    display(isPalindrome);


}

the problem lies in that you aren't doing anything with the value you get back from check() . After you call it in main() , the value disappears since you don't assign it to any thing. Then in display() you create a new instance of isPalindrome and set it to false . this one has nothing to do with the one declared in check() . this one will always be false because you don't tell it otherwise. to fix it change your main() method's body to:

// Declare variables
int number;
boolean isPalindrome;  //declare var

// Call method
number = retrieveInput();
isPalindrome = check(number);  //assign it the value
display(isPalindrome);     //pass this value on to display

and then your display to

public static void display(boolean isPalindrome){

    // Declare variable
                           //don't need to decalre var as 
                           //it is now a parameter to the function
   //rest of method as before   
}

This will allow your display to know whether or not the number is a palindrome or not.

Edit Also you are doing if's wrong !

after an if statement there is no semicolon!!

as aa result your' if's in check do absolutely nothing and will always return false! also keep in mind what would happen with the number 12325. it is not a palindrome but it's first and last digits are the same whereas the second and forth are. neither of your if statements would evaluate as true if they were correct. the following is the fixed code:

import javax.swing.JOptionPane;

public class Palindrome {
    // Main Method
    public static void main(String[] args) {
        // Declare variables
        int number;
        boolean isPalindrome; //add this var to store the value

        // Call method
        number = retrieveInput();
        isPalindrome = check(number);   //assign it so we know wheter it a palindrome or not
        display(isPalindrome);          //pass it to display() so it mknows wheter it is a palindrome or not
    }// End main method
    //*************************************************************************************
    // Method to retrieve the input

    public static int retrieveInput() {
        //Declare variables
        String number;
        int numInput = 0;
        boolean done = false;

        // Prompt user for input and validate that input
        while (!done) {
            try {
                number = JOptionPane.showInputDialog("Enter 5 single digits");
                numInput = Integer.parseInt(number);
                if (numInput < 10000 || numInput > 99999) {     //don't throw an error, inefecient just show the error instead
                    JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
                } else {
                    done = true;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Entry invalid. Please re-enter 5 single digits", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        return numInput;

    }// End retrieveInput method
    //*************************************************************************************
    // Method to determine if input is a palindrome or not

    public static boolean check(int number) {
        //Declare variables
        int num1,num2,num4,num5;
        boolean isPalindrome;

        num1 = number / 10000;
        num2 = number % 10000 / 1000;
        num4 = number % 100 / 10;
        num5 = number % 10;

        // Checking to see if input is a palindrome
        if (num1 == num5 && num2 == num4){  //no semicolons!!! else the if does nothing
            isPalindrome = true;            // and it evaluateds whaat it was supposed to like normal code
        }else{
            isPalindrome = false;
        }

        return isPalindrome;
    }// End check method
    //*************************************************************************************
    // Method to display results

    public static void display(boolean isPalindrome) { // no variables to declare as it now a parameter
        // Display results
        if (isPalindrome == true) {
            JOptionPane.showMessageDialog(null, "These 5  digits are a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "These 5  digits are NOT a palindrome", "Results", JOptionPane.INFORMATION_MESSAGE);
        }
    } // End display method 
    //************************************************************************************* 
} // End class Palindrome  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM