0

I am trying to check to see if, entered a string, that the string is a palindrome

if it is display something positive if not... something negative (invalid)

I am currently getting the answer invalid both times (no matter what is entered) i'm not quite sure if there is a problem with the for loops or the boolean statement.

    //ACTION FROM BUTTON PERFORMED HERE
private void ButtonActionPerformed(ActionEvent evt) {
    //creating variables
    String myString = textField1.getText();
    int stringLength = myString.length();
    char arrayOne[] = new char[stringLength];
    char arrayTwo[] = new char[stringLength];
    boolean palindrome = false;

    //for loop to setup ARRAY ONE
    for(int i = 0; i < stringLength-1; i++){
        arrayOne[i] = myString.charAt(i);
    }

    //for loop to setup ARRAY TWO
    for(int i = stringLength-1; stringLength-1 > i; i--){
        arrayTwo[i] = myString.charAt(i);
    }

    //for loop checking if array indexes are equivalent in value (char)
    for(int i = 0; i < stringLength-1; i++){
        if(arrayOne[i] != arrayTwo[i]){
            palindrome = false;
        }
        else{
            palindrome = true;
        }
    }

    //assigning text to the text boxes based on boolean palindrome
    if(palindrome == true){
        textField2.setText("Valid");
    }
    if(palindrome ==false){
        textField2.setText("Invalid");
    }
}

}

i think i commented it descently

Mordechai
  • 15,437
  • 2
  • 41
  • 82
relativeLee
  • 39
  • 2
  • 7

8 Answers8

3

Change

for(int i = stringLength-1; stringLength-1 > i; i--)

to

for(int i = 0; i < stringLength-1; i++)

and change

for(int i = stringLength-1; i-1 > 0; i--)

to

for(int i = stringLength-1; i-1 >= 0; i--)

EDIT:

That was a debugging fest!!

Here is a working code:

    String myString = textField1.getText();
    int stringLength = myString.length();
    char arrayOne[] = new char[stringLength];
    char arrayTwo[] = new char[stringLength];
    boolean palindrome = true;
    //for loop to setup ARRAY ONE
    for(int i = 0; i <= stringLength-1; i++){
        arrayOne[i] = myString.charAt(i);
    }

    //for loop to setup ARRAY TWO
    for(int i = stringLength-1, pos = 0; i >= 0; i--, pos++){
        arrayTwo[pos] = myString.charAt(i);
    }

    //for loop checking if array indexes are equivalent in value (char)
    for(int i = 0; i <= stringLength-1; i++){
        if(arrayOne[i] != arrayTwo[i]){
            palindrome = false;
            break;
        }
    }

    //assigning text to the text boxes based on boolean palindrome
    if(palindrome == true){
          textField2.setText("Valid");
    }
    else{
        textField2.setText("Invalid");
    }
Aditi
  • 1,188
  • 2
  • 16
  • 44
2

I agree with the other answers about your error, but I think a more concise solution would be

boolean isPalindrome(String myString) {    
    int n = myString.length;
    for( int i = 0; i < n/2; i++ )
        if (myString.charAt(i) != myString.charAt(n-i-1)) return false;
    return true;    
}

Your code would now be

private void ButtonActionPerformed(ActionEvent evt) {
    String myString = textField1.getText();     
    textField2.setText( isPalindrome(myString) ? "Valid" : "Invalid" );
}
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
  • i definitely like this - but it doesn't have both of the arrays, should i call this seperately – relativeLee Feb 22 '13 at 06:30
  • You don't need arrays at all. I suggest not even using them when you can access the characters of the string one by one anyway. – Andrew Mao Feb 22 '13 at 06:33
1

This loop copies all characters except the last one which probably is not what you wanted:

//for loop to setup ARRAY ONE
for(int i = 0; i < stringLength-1; i++){
    arrayOne[i] = myString.charAt(i);
}

It should probably be fixed like this:

//for loop to setup ARRAY ONE
for(int i = 0; i < stringLength; i++)
{
    arrayOne [i] = myString.charAt (i);
}

Body of this loop:

//for loop to setup ARRAY TWO
for (int i = stringLength-1; stringLength-1 > i; i--)
{
    arrayTwo [i] = myString.charAt (i);
}

is never executed, because initial value of i: stringLength - 1 does not satisfy loop condition: stringLength - 1 > i.

You should probably change it to be:

// For loop to setup ARRAY TWO
for (int i = 0; i < stringLength; i++)
{
    arrayTwo [i] = myString.charAt (stringLength - i - 1);
}

Also, after this loop:

// for loop checking if array indexes are equivalent in value (char)
for (int i = 0; i < stringLength-1; i++)
{
    if (arrayOne [i] != arrayTwo [i])
    {
        palindrome = false;
    }
    else
    {
        palindrome = true;
    }
}

variable palindrome will contain result of last comparison only, so if all characters except the last ones were different but last characters were equal, palindrome will be true which is probably not what you wanted. Probably you should change the code like this:

palindrome = true;
for (int i = 0; i < stringLength; i++)
{
    if (arrayOne [i] != arrayTwo [i])
    {
        palindrome = false;
    }
}

Note that I also changed stringLength - 1 to stringLength, otherwise you were ignoring last characters.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • I see - then if i were to change the second part "stringLegnth-1>i" to "i>0" would it satisfy the condition or make it forever true – relativeLee Feb 22 '13 at 06:27
1
//for loop to setup ARRAY TWO
for(int i = stringLength-1; stringLength-1 > i; i--){
    arrayTwo[i] = myString.charAt(i);
}

This falls over after the first iteration.

You need to change it to something like:

//for loop to setup ARRAY TWO
for(int i = stringLength-1; i > 0; i--){
    arrayTwo[i] = myString.charAt(i);
}
mcalex
  • 6,628
  • 5
  • 50
  • 80
0

The easiest way to test for a palindrome in java

String str = "Able was I ere I saw Elba"
boolean palindrome = str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());

Yep, that's it.

Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
0
public static void main(String[] args) {
    String s = "akaq";
    boolean b = false;
    for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
        if (s.charAt(i) == s.charAt(j)) {
            b = true;
            continue;
        } else {
            b = false;
            break;
        }
    }
    if (b)
        System.out.println("Palindrome");
    else
        System.out.println("Not Palindrome");
}

Try something like this instead of 2-3 for loops.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

Change the first for loop from stringLength-1 to just stringLength because you are using < and not <=

Change the second for loop to

if(int i = stringLength-1; i>=0; i--)

Also, set palindrome to true by default and remove the

else{
    palindrome = true;
}

part because now if the first and last characters of the loop are the same, but not the middle, it will return true.

EDIT: Also the third for loop should be stringLength and not stringLength-1 because you are using < and not <=

Holden Lewis
  • 387
  • 3
  • 18
0

There's no need to copy everything into arrays. String is basically an array itself. You can access the individual characters with charAt().

Also there is no need to loop the entire length of the String since equality is associative.

So simply use :

public boolean isPalindrome(String s) {
    for (int i = 0; i < s.length() / 2; i++) {                  // only until halfway
        if (s.charAt(i) != s.charAt(s.length() - i - 1)) {      // accessing characters of String directly
            return false;
        }
    }
    return true;
}

One last remark : if the String's length is odd, you don't need to check the middle chracter. So in the code above the diision by

bowmore
  • 10,842
  • 1
  • 35
  • 43