2

I want to ignore special characters like , " ; : using string tokenizer. For example, if I enter:

He said, "That's not a good idea."

output should be:

He
Said
that
s
not
a
good
idea

This is my current code

class MyClass
{
    public static void main(String[] argv)
    {
        System.out.print("Enter text to break :- ");
        Scanner sc = new Scanner(System.in);
        String x = sc.nextLine();
        StringTokenizer url = new StringTokenizer(x, " ");              
        
        while(url.hasMoreTokens())
        {
            System.out.println(url.nextToken());
        }
    }
}
slartidan
  • 20,403
  • 15
  • 83
  • 131
nascar895
  • 149
  • 1
  • 12
  • Consider [`String#split`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) with the appropriate regex. – bradimus Aug 19 '16 at 14:41

1 Answers1

1

You can replace that special characters with regex like this:

class Q_03 {
    public static void main(String[] argv) {
        System.out.print("Enter text to break :- ");
        Scanner sc = new Scanner(System.in);
        String x = sc.nextLine().replaceAll("[|;:,'\"]", " ");
        StringTokenizer url = new StringTokenizer(x, " ");

        while (url.hasMoreTokens()) {
            System.out.println(url.nextToken());
        }
    }
}

You can add to this regexp "[;:,'\"]" whatever symbols you want, but some special symbols (like ") must have been escaped with backslash \".

DimaSan
  • 12,264
  • 11
  • 65
  • 75