Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is StringTokenizer still recommended for USACO?

Tags:

java

parsing

I am just beginning to tackle some USACO practice problems using Java. However, I've immediately encountered some confusion regarding parsing file input...

The USACO training pages say:

Important: BufferedReader and StringTokenizer are far more efficient than many other schemes for reading input. They can make a huge difference in the efficiency of your program! Use them!

However, the Oracle docs say:

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.

I am finding very mixed opinions on the internet. Some, including the training pages, say that StringTokenizer should be used because it's faster than String.split() which is faster than Scanner (though this answer disagrees). Others say Scanner or String.split() should be used because they're simpler to use and because the performance cost from parsing files is negligible.

Which method is the most practical as of now?

like image 943
John Qian Avatar asked Sep 29 '22 10:09

John Qian


1 Answers

In programming competitions, the logic and algorithmic part of your code is more important than the method you use to get input data.

There are only a few milliseconds of difference between Scanner and StringTokenizer which does not impact your solution in any major way.

In general, however, Scanner is easier to use and performs consistently well whereas (you stated) StringTokenizer is a legacy class and its use is discouraged.

like image 185
richperson Avatar answered Oct 15 '22 10:10

richperson