Han

Han Ferik

Home | Projects | AP CSA

Project Palindrama

Tools: VSCode | Source Code: GitHub

The Palindrama project is a palindrome-detection tool that helps users identify and extract palindromes from a given sentence. A palindrome is a word or phrase that reads the same backward as forward, like "hannah" or "racecar." In this project, users input a sentence, and the program processes it to find the longest palindromes by comparing characters from the front and back. The tool effectively tracks multiple palindrome candidates and then returns the longest one, enhancing the user’s ability to discover hidden patterns in text. This approach ensures an engaging exploration of palindromes in a user-friendly manner.

In addition to detecting palindromes, Palindrama includes a sorting feature to organize the found palindromes by length. This allows users to not only view all detected palindromes but also focus on the longest ones. The program also takes into account both lowercase and uppercase letters, ensuring accuracy regardless of character casing. By offering these features, the project provides a fun and educational tool for anyone interested in exploring palindromes, whether for linguistic curiosity or problem-solving practice.

To complete the Palindrama project, I had to learn several foundational concepts in Java programming. I needed to understand how to take input from the user using the `Scanner` class and how to implement both `while` and `while not` loops to control the flow of my program. I also learned how to use `if` statements to make logical decisions and how to create and work with `ArrayList` objects in Java. Additionally, I explored how to get the size of an `ArrayList`, access individual elements by index, and find the length of an array. Each of these components was essential to manage the dynamic input and processing required for detecting palindromes.

Moreover, I had to dive deeper into algorithms, particularly learning how to implement a bubble sort to organize the palindromes by length, ensuring the longest one was identified and printed. Another key part of the project involved accessing the last index of an `ArrayList`, as this was crucial for my sorting and comparison logic. I also learned the importance of resource management in Java, discovering how to handle a "Resource leak: 'inputPalindrama' is never closed" error, which taught me the best practices for using the `Scanner` class. Finally, writing methods to modularize the code helped me structure the logic more efficiently.

Line 1 to 2: Importing Classes

Line 4: Creating our Palindrama Class

Line 5: Creating our main method

Line 7 to 9: Creating a new constructor in Scanner Class, the one we imported, and taking input from the user for the palindrama

Line 11 to 16: Declaring the variables that we are going to use, and creating a list to keep the palindromes if there are more than 1

Line 19: Creating a while loop until the length of our input will be smaller than the counter we use from 0 to the last index, this is done to make sure that our code will stop after looking over all of the characters in the input

Line 20: Resetting the counter that starts from the last index to the beginning, this is done to make sure that after increasing counter from the front by one, it will again check for every single possibility it can have again

Line 21: Creating a while loop where the counter from the last index is smaller than the whole length of the sentence, this is done to make sure that our counter from the last input will stop after passing the first index

Line 23: We are declaring a starting index which is zero

Line 24: We are declaring a reversed starting index which will start from the back of the sentence and we are subtracting the last index with -1 so that it doesn't cause an error called IndexOutOfBoundsException because indexes start from zero but not 1, so we need to subtract 1

Line 25: We are creating an if statement to check the value of starting index is smaller or equal to ending index since it must be smaller than it, if not, it will pass the valid substring range

Line 26: We are again creating an if statement by using the method charAt to check whether our first index and the last index are equal

Line 28 to 29: After making sure that our characters are the same, it will make our counter that we use for checking the second character from the back and front of the sentence to check whether they are the same or not. The reason we didn't use the same counter is that if we don't get the same number, we would just need to decrease the counter with the times that we used. If the time was more than 1 like 5 or 10 or 100, it would be just hard to find that exact number to subtract, which is why we create a new counter to check, where we can just easily equal our first counter with these if all characters are the same afterward

Line 30 to 31: We are creating a while loop where our second counters don't pass their boundaries, and the characters are the same

Line 32 to 33: We will increase our counters to check whether it is again the same at the next characters of the next indexes

Line 35: We are creating an if statement to check whether our front counter passed the back counter, and if it's true, this shows that the whole sentence between those ranges is the same

Line 36: If the condition above is correct, then we will just equal our palindrome from the beginning of the index and the ending index + 1. It is +1 because else it won't be in the boundaries of the sentence since the substring method doesn't take the last word

Line 37 to 38: We are again creating an if statement to check if there is the same palindrome that we already got in our list, and if there isn't, we will just add our palindrome into our list

Line 43: We will increase our back counter with 1 so that it will go to the next letter in the last index while the front counter remains the same if there are no equal matches

Line 45: We will increase our front counter with 1 so that it will go to the next letter in the first index while the back counter resets to 0. If there are no equal matches, we reset our back counter to zero because before we increased our first index, the back counter was already at index 0, so we need to reset it to start again from the last index so that it can again check for more palindromes while staying within the range of boundaries. Since if we didn't reset it, it would be out of the range

Line 49 to 69: We are making a bubble sort

Line 49: We are creating an if statement to check if there are more than 1 element in the palindrama list, and if so, we need to sort them from small to large

Line 51: We are initializing a boolean called swapped to make sure that if our letters have swapped, it will just take the largest one from our list

Line 52: We are creating a do condition which will make the for loop work until all of the letters are swapped

Line 53: At the beginning, our swapped method will be false, and after all of the words are swapped, it will change to true

Line 54: We are creating a for loop which will go until all elements in the list are sorted

Line 55 to 59: We will first check the length of our first index and the second index, and if it's smaller than the second index, it will first copy our first index into another variable, and then it will change the positions of our variables, making the bigger number at the front. It will then increase the index to check the next indexes

Line 62 to 72: If the swapped condition is true, then it will display the longest palindrome in the list of palindromes by taking the last index of it. If there is just one element in the list, then it will just take the first index, and else, it just means that there are no indexes, so it will just print that there are no palindromes in our list


Palindrama Java Code

Palindrama Java Code


		import java.util.Scanner;
		import java.util.ArrayList;
		
		public class Palindrama {
		    public static void main(String[] args) {
		        Scanner inputPalindroma = new Scanner(System.in);
		        System.out.println("Enter your sentence:");
		        String outputPalindroma = inputPalindroma.nextLine();
		        System.out.println("Your sentence is: " + outputPalindroma);
		
		        int counterBack = 0;
		        int counterFront = 0;
		        int counterBack1 = 0;
		        int counterFront1 = 0;
		        int lastIndex = 0;
		        String copyOfIndex;
		        ArrayList<String> palindramaList = new ArrayList<>();
		        
		        // Loop through the sentence
		        while (counterFront < outputPalindroma.length()) {
		            counterBack = 0; // Reset counterBack for each new start position
		            while (counterBack <= outputPalindroma.length() - counterFront - 1) {
		                // Ensure indices are valid
		                int startIndex = counterFront;
		                int endIndex = outputPalindroma.length() - counterBack - 1;
		                if (startIndex <= endIndex) { // Ensure valid substring range
		                    if (outputPalindroma.charAt(startIndex) == outputPalindroma.charAt(endIndex)) {
		                        // Check if substring is a palindrome
		                        counterFront1 = startIndex;
		                        counterBack1 = endIndex;
		                        while (counterFront1 <= counterBack1 &&
		                               outputPalindroma.charAt(counterFront1) == outputPalindroma.charAt(counterBack1)) {
		                            counterFront1++;
		                            counterBack1--;
		                        }
		                        if (counterFront1 > counterBack1) {
		                            String palindrome = outputPalindroma.substring(startIndex, endIndex + 1);
		                            if (!palindramaList.contains(palindrome)) {
		                                palindramaList.add(palindrome);
		                            }
		                        }
		                    }
		                }
		                counterBack++;
		            }
		            counterFront++;
		        }
		
		        // Original sorting method
		        if (palindramaList.size() > 1) {
		            // Bubble sort for sorting based on length
		            boolean swapped;
		            do {
		                swapped = false;
		                for (int i = 0; i < palindramaList.size() - 1; i++) {
		                    lastIndex = palindramaList.size();
		                    if (palindramaList.get(i).length() > palindramaList.get(i + 1).length()) {
		                        copyOfIndex = palindramaList.get(i);
		                        palindramaList.set(i, palindramaList.get(i + 1));
		                        palindramaList.set(i + 1, copyOfIndex);
		                        swapped = true;
		                    }
		                }
		            } while (swapped);
		
		            // Display the longest palindrome
		            System.out.println("The longest palindrome in the list is: " + palindramaList.get(lastIndex));
		        } else if (palindramaList.size() == 1) {
		            System.out.println("The longest palindrome is: " + palindramaList.get(0));
		        } else {
		            System.out.println("There is no palindrome in your sentence.");
		        }
		
		        inputPalindroma.close(); // Close the scanner to avoid resource leak
		    }
		}
		    

Find me on the interwebs!

Github LinkedIn Instagram Facebook