0

I have a question regarding the Difference between StringTokenizer class and java.util.Scanner class? Though both are used for Dissection (tokenization) purpose. Which one is better to use and have better efficiency? Are these two java classes are alternatives for each other or have separate purposes?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
naeemgik
  • 2,232
  • 4
  • 25
  • 47

3 Answers3

2

From StringTokenizer javadoc

The string tokenizer class allows an application to break a string into token.... The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings...

From Scanner javadoc

A simple text scanner which can parse primitive types and strings using regular expressions.

So Scanner unlike StringTokenizer have methods like nextInt, nextBoolean etc. While Scanner is usefull in some cases when you need to parse user input containig numbers, StringTokenizer in most cases can be replaced with org.apache.commons.lang.StringUtils.split - which doesn't use regular expressions and is pretty fast.

SirVaulterScoff
  • 704
  • 1
  • 6
  • 18
1

From the JavaDoc:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
  • As i stated in my answer String.split is rather slow concerning high load applications. StringUtils.split is way faster. – SirVaulterScoff Oct 26 '11 at 06:27
  • I think split method of java.util.regex.Pattern Class will be more better than the two – naeemgik Oct 26 '11 at 06:27
  • It's just about 10x slower than StringUtils.split. – SirVaulterScoff Oct 26 '11 at 06:40
  • 2
    @Naeem : String.split just uses the Pattern.split method, so it can't be better than the two. StringUtils.split is much faster because it doesn't use a regex. BTW, they changed the implementation in Java 7 to avoid using a regex when the pattern argument is simple and thus doesn't need one. – JB Nizet Oct 26 '11 at 06:43
1

One big difference is that a Scanner can operate on input streams, so you do not need to have it in memory all at once (which in some cases is not even possible, for example when continuously reading user input from a console).

Thilo
  • 257,207
  • 101
  • 511
  • 656