0

I'm trying to write a method to take in a string as a parameter and remove all whitespaces and punctuation from it so this is my idea of how to do that..

    import java.util.*;
public class Crypto {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Please insert the text you wish to encrypt: ");
        String text = input.nextLine();
        text = normalizeText(text);
        System.out.println(text);
    }

    public static String normalizeText(String s){
            s.replace(" ","");
            s.replace("(","");s.replace(")","");s.replace(".","");
            s.replace(",","");s.replace("?","");s.replace("!","");
            s.replace(":","");s.replace("'","");s.replace("\"","");
            s.replace(";","");

            s.toUpperCase();
            return s;



    }
}

Now , I only added the text = normalize Text(text); and then printed it because it wouldn't print it to the screen without it( even though in some methods the return would actually show an output on the screen) anyway, even this change didn't help because it doesn't remove anything from the string taken in by the method it prints out the exact same string.. any help? Thanks in advance . :)

4

2 Answers 2

3

Problem in your code is, you haven't assigned back the new string that got generated after s.replace(":",""); Remember, strings are immutable so the change by replace method will not apply to the string object on which you call the method.

You should have written,

s = s.replace(":", "")

Instead of your tedious method normalizeText you can write your method like this,

public static String normalizeText(String s){
    return s.replaceAll("[ ().,?!:'\";]", "").toUpperCase();
}
3
  • Why did you use multiple \\ in the old char position? Thanks for your reply
    – giorgioh
    Oct 13, 2018 at 16:25
  • aah sorry I thought you wanted to replace backslash too. In java if you actually need to replace a backslash then you need to escape it with \ Oct 13, 2018 at 16:27
  • Updated my regex to exactly replace only characters you wanted. Although if you want to retain only alphabets and space in the string then you can write, s.replaceAll("[^ a-zA-Z]", "").toUpperCase(); Oct 13, 2018 at 16:31
1

You need to make assignments to the string after each replacement has been made, e.g.

public static String normalizeText(String s) {
    s = s.replace(" ", "");
    s = s.replace("(","");
    // your other replacements

    s = s.toUpperCase();

    return s;
}

But note that we can easily just use a single regex to handle your replacement logic:

public static String normalizeText(String s) {
    s = s.replaceAll("[().,?!:'\"; ]", "").toUpperCase();

    return s;
}
2
  • Why did you use 4 backslashes \ in the old char position? Thank you for your reply
    – giorgioh
    Oct 13, 2018 at 16:26
  • @lidorportal Because we want to include a literal backslash. Oct 14, 2018 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.