0

This is my first submission, I have completed one quarter of Java programming.

I have an assignment to create a Palindrome Checker. Fairly straight forward, I had that portion of the code figured out in the first hour. However, in typical fashion (for me) I want my code to do a bit more. This is where I run into issues.

I want this code to do the following: Take user input Correctly identify if the input is a palindrome regardless of case or punctuation Run in a loop so that multiple tests can be performed

So far it works, I just don't know if I went about it the right way. Would anyone be willing to take a look and let me know how inefficient this is, or if it has any obvious rookie mistakes? Thanks so much.

Code:

  /**
 * Created by Travis on 1/10/2015.
 */

import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.lang.StringBuilder;

public class Palindrome
{
    public static void main(String[] args) throws IOException //main class
    {
        String str = "", answer = "", test1 = "yes", test2 = "no";  //strings

        int len = 10;  //initial value for len so it doesn't trip the success if

        Scanner KB = new Scanner(System.in);  //user input

        System.out.println("Greetings, Welcome to the Palindrome Checker.\n" +  //initial greeting
                "Would you like to check a Palindrome? (Yes or no)");

        answer = KB.nextLine(); //input is for the sentinel program.

        if (!(answer.equalsIgnoreCase(test1) || answer.equalsIgnoreCase(test2)))  //error message in case user inputs incorrect string.
        {
            System.out.println("Error! You can only choose 'yes' or 'no'.  Please try again:");
            answer = KB.nextLine();  //allows for new answer
        }

        System.out.println(answer + " y"); //debugging so i can see answer

        while (answer.equalsIgnoreCase(test1)) //compares to test1 which is: yes.  as long as answer equals yes, the program should loop
        {
            System.out.println("Please provide a word or phrase:");

            str = KB.nextLine(); //prompt for palindrome

            String str2 = str.toLowerCase().replaceAll("\\s+", "").replaceAll("\\W+", "");  //converts input to a string that is a single group of chars with no space or punctuation

            System.out.println(str2);  //debug

            StringBuilder str1 = new StringBuilder(str2);  //takes the string and makes a stringbuilder so i can delete chars to test

            len = str1.length(); //sets for length.  This lets me account for any length of phrase

            System.out.println(len); //debug

            System.out.println(str1); //debug

            for (int i = 0; len >= 2; i++) //for loop to progressively test front and back letters and proceed if they are the same
            {
                char ch1 = str1.charAt(0);  //takes the letter at [0] and converts to char
                char ch2 = str1.charAt(len - 1);  //takes the last letter and converts to char

                System.out.println("The first letter in your phrase is: " + ch1);  // lists the letter at [0]
                System.out.println("The last letter in your phrase is: " + ch2);  //lists the letter at the end

                if (!(ch1 == ch2))  //if the front and back letter do not match, fails and prompts for new phrase
                {
                    System.out.println("Sorry, this phrase is not a palindrome. \n" +
                            "Would you like to try again?");
                    answer = KB.nextLine();
                    break;
                }

                else if (ch1 == ch2) //if front and back do match, removes front and back letters and updates stringbuilder
                {
                    System.out.println("Removing letters on each end of the phrase and performing " +
                            "new check:\n");
                    str1.deleteCharAt(len - 1);  //deletes the last letter from str1
                    str1.deleteCharAt(0);  //deletes first letter from str1
                    len = str1.length();  //updates len with the new length of str1
                    i++;
                }
            }

            if (len <= 1) //if the for loop successfully reduces stringbuilder to 1 or less characters, prompts for success
            {
                System.out.println("Congratulations, your phrase: '" + str + "' is a palindrome! \n\n" +
                        "Would you like to try again?");
                answer = KB.nextLine();
                len = 10; //resets len
            }
        }

        if (answer.equalsIgnoreCase(test2)) //ends sentinel loop.
        {
            System.out.println(answer + " N");
            System.exit(0);
        }
   }
}    
4

0 回答 0