简体   繁体   中英

Java Palindrome assignment - boolean always returns False value

everyone. I am having issues with an assignment for my Java programming class and would like your opinion on it.

The assignment for the method pA is to create a method using only String and Character methods. As such, I have programmed the following methods and when I attempt to call the method pA , it automatically returns a false value. I am thinking there may be something wrong with my for loop but as I am not sure, I figured I would ask on here to see what you have to say.

Thank you in advance, for your time.

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in); //Creates Scanner object to take user input

    String pal; //String to contain user input

    boolean test = false; //boolean to test if user input is a palindrome.

    System.out.println("This program determines if your input is a palindrome!");
    System.out.println("Please enter your input to see if it's a palindrome.");
            pal = keyboard.nextLine();

    pA(pal, test);
    if (test == true)
    {
        System.out.println("Congratulations! You have a palindrome!");
    }
    else if (test == false)
    {
            System.out.println("Unfortunately, you do not have a palindrome.");
    }

}

public static boolean pA(String pal, boolean test) //Part A of Assignment 7.
{
    int l = pal.length(); //integer to contain length of string
    int i; //Loop control variable
    test = false;

    for (i = 0; i < l/2; i++) //for loop that tests to see if input is a palindrome.
    {
        if (pal.charAt(i) == pal.charAt(l-i-1)) //Compares character at point i with respective character at the other end of the string.
        {   
            test = true; //Sets boolean variable to true if the if statement yields true.
        } else 
            test = false; //Otherwise, a false value is returned.
             break;
    }
    return test; //Return statement for boolean variable
} //End pA

From here, when I attempt to Run the program, I get the following message when using the input "tubbut":

run:

This program determines if your input is a palindrome!

Please enter your input to see if it's a palindrome.

tubbut

Unfortunately, you do not have a palindrome.

BUILD SUCCESSFUL (total time: 2 seconds)

You are ignoring the result of your call to the pA method, so the test variable in main is unchanged. Also, there is no reason to pass in test to pA , because then pA only has a copy of the value anyway.

In main, try

test = pA(pal);

And in pA , remove the test parameter; you can make it a local variable.

public static boolean pA(String pal) //Part A of Assignment 7.
{
   int l = pal.length(); //integer to contain length of string
   int i; //Loop control variable
   boolean test = false;  // *** Now it's a local variable
   // Rest of method is here.
}

Additionally, in main , test is already boolean , so you don't need to compare it against true or false . You can replace

if (test == true)
{
    System.out.println("Congratulations! You have a palindrome!");
}
else if (test == false)
{
    System.out.println("Unfortunately, you do not have a palindrome.");
}

with

if (test)
{
    System.out.println("Congratulations! You have a palindrome!");
}
else
{
    System.out.println("Unfortunately, you do not have a 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