简体   繁体   中英

Java, cant figure out how to strip symbols from a String for a Palindrome

Im in highschool and this is an assignment i have, you guys are out of my league but im willing to learn and understand. I looked all over the place but all i could find was complicated syntax i dont know yet. This is what i have, it takes a String and reverses it. I managed to get it to ignore Capitals, but i cannot figure out how to make it ignore symbols. The numbers i have there are from the ANSI Characters, there is a list on textpad im using. Dont be afraid to be harsh, im not good at this and i only want to improve so have at it.

import java.util.Scanner;
public class PalindromeV2
{
    public static void main(String[] args)
    {
        //declare
        Scanner sc = new Scanner(System.in);
        String fwd, rev;
        String result;
        //input
        System.out.println("What word would you like to Palindrome test?");
        fwd = sc.next();
        rev = reverseString(fwd);
        result = stripPunctuation(fwd);

        if(stripPunctuation(rev).equals(stripPunctuation(fwd)))
        {
            System.out.println("That is a palindrome");
        }
        else
            System.out.println("That is not a palindrome");

    }
    public static String reverseString(String fwd)
    {
        String rev = "";
        for(int i = fwd.length()-1; i >= 0; i--)
        {
            rev += fwd.charAt(i);
        }
        return rev.toUpperCase();
    }

    public static String stripPunctuation(String fwd)
    {
        String result = "";
        fwd = fwd.toUpperCase();

        for(int i = fwd.length()-1; i >= 0; i--)
        {
            if((fwd.charAt(i)>=65 && fwd.charAt(i)<=90)||(fwd.charAt(i) >= 48 && fwd.charAt(i) <= 58));
            result = result + fwd.charAt(i);
        }
        return result;
    }
}

You can use this as a checking condition

if (Character.isLetter(fwd.charAt(i)) {
    // do something
}

This will check to make sure the character is a letter, so you don't have to worry about case, numbers, or other symbols.

If you want to strip your string out of some set of characters than do something like that

clearString=targetStringForStripping.replaceAll([type_characters_for_stripping],"");

this will remove all characters you will provide inside square brackets. There is even more. If you want to let say leave only letters (because in palindromes nothing matters except letters - spaces are not important to) than you simply can use predefine character set - letters. To conclude all if you do

clearString=targetStringForStripping.replaceAll("[\\w]","");

or

clearString=targetStringForStripping.replaceAll("[^a-zA-Z]","");

you will get clear string with white characters in first example, and only letters in second one. Perfect situation for isPalindrom resolution.

if((fwd.charAt(i)>=65 && fwd.charAt(i)<=90)||(fwd.charAt(i) >= 48 && fwd.charAt(i) <= 58));

you have semicolon at last. so i think if condition is no use here

Since this is a highschool assignment, I'll just give some pointers, you'll figure it out on your own.

  1. Think about what you want to include / exclude, then write the code. Keep in mind, that you can compare char variables using < or > operators as long as you do not want to handle complex character encodings.

  2. A String is really just a sequence of chars which one by one you can compare or reorder, include or exclude.

  3. A method should only do one thing, not a lot of things. Have a look at your reverseString method. This is doing an toUpperCase to your string at the same time. If your programs get more complex, this way of doing things is not to easy to follow.

Finally, if you eg just want to include capital letters in your palindrome check, then try some code like this:

char[] toCheck = fwd.toCharArray();
for (char c : toCheck) {
  if (c >= 'A' && c <= 'Z') {
    result = result + c;
  }
}

Depending on your requirements this might do what you want. If you want something different, have a look at the hints I gave above.

Java golf?

public static String stripPunctuation(String stripThis) {
    return stripThis.replaceAll("\\W", "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM