javastringremoveall

Java: removing numeric values from string


I have suceeded with the help of this community in removing numeric values from user input, however, my code below will only retrieve the alpha characters before the numeric that has been removed:

import java.util.Scanner;

public class Assignment2_A {

    public static void main(String[] args) {
        Scanner firstname = new Scanner(System.in);
        String firstname1 = firstname.next();
        firstname1 = firstname1.replaceAll("[^A-Z]","");
        System.out.println(firstname1);
    }
}

For example if user input = S1234am, I am only getting back: S. How do I retrieve the remaining characters in the string?


Solution

  • Your regular expression [^A-Z] is currently only configured to preserve upper-case letters. You could try replacing it with [^A-Za-z] to keep the lower-case letters too.