You can read the lines and replace all special characters safely this way.
Keep in mind that if you use \\W
you will not replace underscores.
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
System.out.println(scan.nextLine().replaceAll("[^a-zA-Z0-9]", "");
}