3
import java.util.Scanner;
public class Ex3 {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please input a word: ");
        String Line = keyboard.nextLine();
        boolean x = isReverse(Line);
        System.out.print("It is " + x + " that this word is a palindrome.");
    }
    public static boolean isReverse(String Line) {
        int length = Line.length();
        boolean x = true;
        String s = "";
        for (int i = 0; i < length; i++) {
            if (Line.charAt(i) != ' ') {
                s += Line.charAt(i);
            }
        }
        for (int i = 0; i < length; i++) {
            if (Line.charAt(i) != Line.charAt(length - 1 -i)) {
                x = false;
            }
        }
        return x;   
    }
}

What I am trying to do is make a program that takes a word or phrase as input and returns true or false depending on if it is a palindrome or not. In the program I am supposed to ignore whitespace and punctuation marks and make palindromes such as "A man, a plan, a canal, Panama." I think I have solved the whitespace problem, but can not figure out how to ignore all punctuation marks.

assylias
  • 321,522
  • 82
  • 660
  • 783
user1730357
  • 355
  • 4
  • 7
  • 15
  • I am using scanner class I am wondering how I would change my code so that any phrase i input gets this done to it. – user1730357 Jan 17 '13 at 00:43

2 Answers2

8

You could use a regular expression to remove all the non-word characters from your string: \\W represents non-word characters

String s = "A man, a plan, a canal, Panama.";
String lettersOnly = s.replaceAll("[\\W]", "");
System.out.println("lettersOnly = " + lettersOnly);

outputs:

lettersOnly = AmanaplanacanalPanama

If you want to reduce the length of your code, you can also use StringBuilder#reverse to reverse the string:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please input a word: ");
    String line = keyboard.nextLine();

    String cleanLine = line.replaceAll("[\\W]", "");
    String reverse = new StringBuilder(cleanLine).reverse().toString();
    boolean isPalindrome = cleanLine.equals(reverse);

    System.out.print("It is " + isPalindrome + " that this word is a palindrome.");
}

EDIT

If you need to stick to the loop, you can simply check in your loop if the characters are letters:

public static boolean isReverse(String Line) {
    int length = Line.length();
    boolean x = true;
    String s = "";
    for (int i = 0; i < length; i++) {
        if ((Line.charAt(i) >= 'a' && Line.charAt(i) <= 'z')
          || (Line.charAt(i) >= 'A' && Line.charAt(i) <= 'Z')) {
            s += Line.charAt(i);
        }
    }

Note: you will have an issue with the case (A != a) - an easy fix is to first put all characters in lower case with String lowerCase = Line.toLowerCase();.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • I am using scanner class I am wondering how I would change my code so that any phrase i input gets this done to it. – user1730357 Jan 17 '13 at 00:36
  • @user1730357 `boolean x = isReverse(Line.replaceAll("[\\W]", ""));` instead of `boolean x = isReverse(Line);`. – assylias Jan 17 '13 at 00:43
  • @user1730357 I have added a shorter and easier version if that helps you. – assylias Jan 17 '13 at 00:47
  • I added that to the boolean x but it still won't work. Also don't quite know what the s totally does as i asked someone else and they said to use the other for loop. – user1730357 Jan 17 '13 at 00:49
  • @user1730357 The `s` in my initial example is your `Line`. – assylias Jan 17 '13 at 00:50
  • I am only in my second semester of java so what you just typed doesn't quite make sense. – user1730357 Jan 17 '13 at 00:52
  • @user1730357 In your code, you simply need to replace `boolean x = isReverse(Line);` by `boolean x = isReverse(Line.replaceAll("[\\W]", ""));` - that will replace all the non-word characters of the input before it is passed to your `isReverse` method. The second example I gave simply relies on the available built-in methods to do the same thing in less lines of code (which generally means less bugs). – assylias Jan 17 '13 at 00:57
  • @user1730357 I have added an additional example that matches your code more closely. – assylias Jan 17 '13 at 01:00
  • I did this in the main method, but it does not seem to work. Also do i even need my first for loop? – user1730357 Jan 17 '13 at 01:02
  • @user1730357 The first loop removes the punctuation, spaces and so on. So yes you need it. I suggest you run your code in a debugger step by step to better understand what each line does. – assylias Jan 17 '13 at 01:03
  • Thanks for your help. I'll take a look at my code now and try what you said. – user1730357 Jan 17 '13 at 01:07
1

The StringUtils class in Apache Commons Lang has some methods that may be handy including deleteWhitespace() and difference(). Passing your string to difference() along with a string of all punctuation characters that you want to remove would return a punctuation free string.

melbyts
  • 189
  • 1
  • 5