Java Poem Palindrome Checker: Iterate through array matching elements in order

2400 views java
3

Currently trying to program a poem Palindrome checker. This is not for palindromes specifically, but that the array has words in the same order both ways. For example the following is a poem palindrome

Life-
imitates nature,
always moving, traveling continuously.
Continuously traveling, moving always,
nature imitates
life

My issue is iterating through the array to match the first and last elements, as currently it compares things in the wrong order.

My code is as follows:

import java.util.Scanner;
import java.io.*;
public class WordPalindromeTest {

    public static void main(String[] args) {
        System.out.println("This program determines if an entered sentence/word poem is a palindrome.");
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a string to determine if it is a palindrome: ");
        while(input.hasNextLine()) {
            String palin = input.nextLine();
            if(palin.equals("quit")) {
                break;
            }
            else {
                boolean isPalin = isWordPalindrome(palin);
                if(isPalin == true) {
                    System.out.println(palin + " is a palindrome!");
                    }
                    else
                        System.out.println(palin + " is NOT  a palindrome!");
                }
            }

        System.out.println("Goodbye!");
        input.close();

    }

    public static boolean isWordPalindrome(String s) {
        boolean isWordPal = false;
        String lowerCase = s.toLowerCase();
        String replaced = lowerCase.replaceAll("[^a-zA-Z0-9\\s]", "");
        String words[] = replaced.split(" ");
        for(int i = 0; i < words.length; i++) {
            for(int j = 0; j < words.length; j++) {
                if (words[i].equals(words[j]) && i != j) {
                    isWordPal = true;
                }
                else
                    isWordPal = false;
            }
        }
        return isWordPal;
    }
}

With the specific point in question being

public static boolean isWordPalindrome(String s) {
        boolean isWordPal = false;
        String lowerCase = s.toLowerCase();
        String replaced = lowerCase.replaceAll("[^a-zA-Z0-9\\s]", "");
        String words[] = replaced.split(" ");
        for(int i = 0; i < words.length; i++) {
            for(int j = 0; j < words.length; j++) {
                if (words[i].equals(words[j]) && i != j) {
                    isWordPal = true;
                }
                else
                    isWordPal = false;
            }
        }
        return isWordPal;
    }

I am confused on how to properly set up the loop to compare the right elements. It should compare the first element to the last, the second to the second to last, etc. until the loop is finished. I realize I have it compare the first to the entire array before moving on.

answered question

The biggest mistake in your code is that there are no comments. You should write your algorithm in English first, in comments. Then you should translate it into code after. Keep the comments. Code should be documented, either self documented (a skill few coders have) by the way it's written, or explained in comments. Like the part where you can get 90% on an exam full of wrong answers, if you show workings. You're new to the sport and probably don't think in code yet. Complex algorithms should always be written in the language you think in, and then translated to the language you're learning.

1 Answer

13

This seems like a homework assignment so I won't give you a working solution. But this of it like this:

-You don't need two loops. You only need to compare the first to the last, the second to the second to last, etc. (Hint: if you subtract i-1 from the length of the Array you'll get the corresponding element to i that you need to compare to). Also you only need to iterate over half of the length of the Array

-If ever isWordPal becomes false, you need to return false. Otherwise it might get overwritten and at the end it will return true.

posted this

Have an answer?

JD

Please login first before posting an answer.