2

I am attempting to create a method in Java which will recognize non alphanumeric symbols in a line of text and remove them. However, I am limited in the tools that I can use. I have to use a loop and a scanner to read through the text, and only using the charAt and substring methods to remove the offending characters. Here is the code that I came up with

public String sanitize2(){
        Scanner sanitizer = new Scanner(input.getText());
        String toBeSanitized = sanitizer.nextLine();
        int length = toBeSanitized.length();
        for (int i=0;i < toBeSanitized.length(); i++){
            if ((toBeSanitized.charAt(i) >= 'a' && toBeSanitized.charAt(i) <= 'z') ||
                (toBeSanitized.charAt(i) >= 'A' && toBeSanitized.charAt(i) <= 'Z') ||
                (toBeSanitized.charAt(i) >= '0' && toBeSanitized.charAt(i) <= '9')) 
            {
                   toBeSanitized = toBeSanitized.substring(0,i) + toBeSanitized.substring(i+1);
            }
        }
        input.setText(toBeSanitized);
        return toBeSanitized;
    }

However it completely gives the wrong answer when i insert text into the input field. Can anyone shed light on what i'm doing wrong here?

Gee Boughuski
  • 53
  • 1
  • 6
  • Check this post. It's duplicated. http://stackoverflow.com/questions/1805518/replacing-all-non-alphanumeric-characters-with-empty-strings – Tran Ho Apr 08 '16 at 01:05
  • @kimdung Not really of duplicate of your linked Question. That one is about regex, this one is not. This one is about single character Unicode number. – Basil Bourque Apr 08 '16 at 01:08
  • I had looked at that as well, but i don't think it helps my situation as i'm limited by what methods I can use – Gee Boughuski Apr 08 '16 at 01:09
  • You're removing characters when they're in the alphanumeric range – jack3694078 Apr 08 '16 at 01:12
  • Your code looks like it's trying to only remove alphanumeric characters, not keep them. Also, use a `while` loop instead of `for`, and only increment `i` when you find a valid character. You're currently always increment long `i` so you'll skip some bad characters – Krease Apr 08 '16 at 01:12
  • can you give us any sample input along with the actual output and the desired output? – JanLeeYu Apr 08 '16 at 01:16

1 Answers1

1

If you are not required to use all the given tools, then I think this might Help solve your problem.

    String toBeSanitized = "(*this*)* text is ^0nly for t3st!ng!";
    String sanitizedStr = "";
    int length = toBeSanitized.length();
    for (int i=0;i < toBeSanitized.length(); i++){
        if ((toBeSanitized.charAt(i) >= 'a' && toBeSanitized.charAt(i) <= 'z') ||
        (toBeSanitized.charAt(i) >= 'A' && toBeSanitized.charAt(i) <= 'Z') ||
        (toBeSanitized.charAt(i) >= '0' && toBeSanitized.charAt(i) <= '9') || 
        toBeSanitized.charAt(i) == ' ') // optional
        {
            sanitizedStr += toBeSanitized.charAt(i);
        }
    }

    System.out.println(sanitizedStr);

I also included the spaces just incase but can just remove it if you want. Hope it helps.

JanLeeYu
  • 981
  • 2
  • 9
  • 24
  • Thanks, this seems to be working well. If i'm reading this right, instead of removing unwanted characters, it makes a new string consisting only of the accepted characters? – Gee Boughuski Apr 08 '16 at 01:35
  • Yes you are right. Just skip them if you don't want them. – JanLeeYu Apr 08 '16 at 01:36