Question

This program runs without throwing any exceptions, but regardless of the input the result is always the same: ""blank" is a palindrome." Every single time the input is a palindrome I was just wondering if anyone had any advice as to why this is happening? Here is the code for the program:

class Palindrome
{
    public static void main(String args[])
    {
        int num = 0;
        int dig1 = 0;
        int dig2 = 0;
        int dig3 = 0;
        int dig4 = 0;
        int dig5 = 0;
        boolean digits = true;

        retrieveInput(num);
        check(num,dig1,dig2,dig3,dig4,dig5);
        display(digits,num);
    }

    public static int retrieveInput(int num)
    {
        String number;
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
        num = Integer.parseInt(number);

        while(num < 9999 || num > 99999)
        {
            JOptionPane.showMessageDialog(null, num + " Is Not A Five Digit Number",
                "ERROR", JOptionPane.ERROR_MESSAGE);        
            number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
            num = Integer.parseInt(number);

        }
        return num;
    }

    public static boolean check(int num,int dig1,int dig2,int dig3,int dig4,int dig5)
    {
        boolean digits;

        dig1 = num / 10000;
        dig2 = num % 10000 / 1000;
        dig3 = num % 10000 % 1000 / 100;
        dig4 = num % 10000 % 1000 % 100 /10;
        dig5 = num % 10000 % 1000 % 100 % 10 / 1;

        if (dig1 == dig5 && dig2 == dig4)
            digits = true;
        else
            digits = false;

        return digits;
    }

    public static void display(boolean digits, int num)
    {
        num = retrieveInput(num);
        if (digits = false)
            JOptionPane.showMessageDialog(null, num + " is a palindrome");
        else
            JOptionPane.showMessageDialog(null, num + " is not a palindrome");
    }
}
Was it helpful?

Solution

On the second line of display(boolean, int), you are attempting to compare a boolean with the = operator. This sets digits to false I think you meant to say:

        if (digits == false)

An if statement needs a boolean for input, i.e. if(bool). You don't need to check if this is true or false like if(bool == true) because bool == true always evaluates to equal bool. Saying bool == true is completely redundant. However, you bool == false always evaluates to the opposite of bool. The ! operator flips the value of the following boolean, e.g. !true evaluates to false. !bool therefore always evaluates to the opposite of bool, and is the same as bool == false.

Therefore, you only need:

        if (!digits)

It is also considered good practice to do it this way.

Another way to make your code even shorter is by using the ternary operator. This isn't necessarily the best thing to do, but I personally like doing it this way:

Replace

if (digits = false)
    JOptionPane.showMessageDialog(null, num + " is a palindrome");
else
    JOptionPane.showMessageDialog(null, num + " is not a palindrome");

with

JOptionPane.showMessageDialog(null, num + " is " + (digits ? "not " : "") + "a palindrome");

For more about the ternary operator, see these pages:

Another thing to note is that you don't need to process the input as an integer, because you never use the number in arithmetic statements; you only compare the digits of the number to each other. In this working code, I processed it as a string, so it is much shorter:

import javax.swing.*;

class Palindrome
{
    public static void main(String args[])
    {
        display();
    }

    /**
     * Returns a five-digit string
     */
    public static String retrieveInput()
    {
        String number = JOptionPane.showInputDialog("Enter A Five Digit Number:"); //get input
        while(number.length() != 5) //continue to get input while it is not five digits
        {
            JOptionPane.showMessageDialog(null, number + " Is Not A Five Digit Number",
                "ERROR", JOptionPane.ERROR_MESSAGE);        
            number = JOptionPane.showInputDialog("Enter A Five Digit Number:");
        }
        return number; //return the input
    }

    /**
     * Returns whether the given five digit string is a palindrome
     */
    public static boolean check(String number)
    {
        return (number.charAt(0) == number.charAt(4) && number.charAt(1) == number.charAt(3)); //checks to see if the first character equals the fifth character and the second character equals the fourth character
    }

    public static void display()
    {
        String number = retrieveInput(); //gets input

        if(check(number)) //if it is a palindrome
        {
            JOptionPane.showMessageDialog(null, number + " is a palindrome"); //say it is a palindrome
        }
        else
        {
            JOptionPane.showMessageDialog(null, number + " is not a palindrome"); //say it isn't a palindrome
        }
    }
}

OTHER TIPS

There are several flaws with your code:

/* The goal of this method is to receive user input and return the
 * entered value. For this you don't need the num input parameter.
 */
public static int retrieveInput(int num)
{
    String number;
    number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
    num = Integer.parseInt(number);

    while(num < 9999 || num > 99999)
    {
        JOptionPane.showMessageDialog(null, num + " Is Not A Five Digit Number",
            "ERROR", JOptionPane.ERROR_MESSAGE);        
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");
        num = Integer.parseInt(number);

    }
    return num;
}

/* Check takes a number and returns if the number is a palindrome or not.
 * The digits of the number are calculated within the method and thus 
 * shouldn't be parameters. (You didn't use these anyway)
 */
public static boolean check(int num,int dig1,int dig2,int dig3,int dig4,int dig5)
{
    boolean digits;

    /* If you removed the paremeters, you've to declare the variables here,
     * e.g. int dig1 = num / 10000
     */
    dig1 = num / 10000;
    dig2 = num % 10000 / 1000;
    dig3 = num % 10000 % 1000 / 100; // This is not needed, because it's not used anywhere
    dig4 = num % 10000 % 1000 % 100 /10;
    dig5 = num % 10000 % 1000 % 100 % 10 / 1;

    /* The pattern you used here is basically
     * if cond == true then return true else return false
     * You never have to use this pattern, since you can
     * directly return conditions
     */
    if (dig1 == dig5 && dig2 == dig4)
        digits = true;
    else
        digits = false;

    return digits;
}

/*
 * This method displays a popup to the user that states if a given number
 * is a palindrome or not. It expects the result and the number as paremeters.
 */
public static void display(boolean digits, int num)
{
    /* You are (again) retrieving the input here, although the input
     * is passed to this method.
     */
    num = retrieveInput(num);

    /* As The Guy with The Hat pointed out in his answer,
     * = is the assignment operator. For comparisons you have to use
     * == instead.
     */
    if (digits = false)
        JOptionPane.showMessageDialog(null, num + " is a palindrome");
    else
        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
}

/*
 * Here is the general control flow of your program
 */
public static void main(String args[])
{
    // These variable declarations can be removed, since you aren't using,
    // nor need them.
    int num = 0;
    int dig1 = 0;
    int dig2 = 0;
    int dig3 = 0;
    int dig4 = 0;
    int dig5 = 0;
    boolean digits = true;

    retrieveInput(num); // you are retrieving the input, but not storing it
    check(num,dig1,dig2,dig3,dig4,dig5); // you are checking, but not storing the result
    display(digits,num);
}

A corrected version:

public static void main(String args[]) {
    int num = retrieveInput();
    boolean isPal = check(num);
    display(num, isPal);
}

public static int retrieveInput() {
    String number = JOptionPane
            .showInputDialog("Enter A Five Digit Number.");
    int num = Integer.parseInt(number);

    while (num < 9999 || num > 99999) {
        JOptionPane.showMessageDialog(null, num
                + " Is Not A Five Digit Number", "ERROR",
                JOptionPane.ERROR_MESSAGE);
        number = JOptionPane.showInputDialog("Enter A Five Digit Number.");

    }
    return num;
}

public static boolean check(int num) {
    int dig1 = num / 10000;
    int dig2 = num % 10000 / 1000;
    // int dig3 = num % 10000 % 1000 / 100;
    int dig4 = num % 10000 % 1000 % 100 / 10;
    int dig5 = num % 10000 % 1000 % 100 % 10 / 1;

    return dig1 == dig5 && dig2 == dig4
}

public static void display(int num, boolean isPal) {
    if (isPal)
        JOptionPane.showMessageDialog(null, num + " is a palindrome");
    else
        JOptionPane.showMessageDialog(null, num + " is not a palindrome");
}

Please notice that I basically didn't change the core of your program. As next steps you might want to check if a user entered non-numerical characters or allow n-length numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top