4

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?

Community
  • 1
  • 1
John Qian
  • 123
  • 9
  • [This IDEOne](http://ideone.com/yjT5j3) should give you an idea of the performance difference. `StringTokenizer` appears to be much faster, but speed isn't everything. No one wants you to write code for them that needs to be updated in a year because it doesn't work with the new JDK release. – christopher Mar 30 '15 at 00:03
  • `Scanner` is effective, easy to use and usually performant enough for the needs of programming competitions. – Louis Wasserman Mar 30 '15 at 00:09

2 Answers2

4

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.

richperson
  • 137
  • 9
0

The most efficient way for Java USACO is BufferedReader and StringTokenizer. Scanner and .split() are less efficient.

Let's say we want to input 3 5, two numbers, N M.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextInt())
int M = Integer.parseInt(st.nextInt())

Inefficient Way Would Be:

Scanner scanner = new Scanner(System.i));
String[] s = scanner.next().split(" ");
int N = Integer.parseInt(s[0]);
int M = Integer.parseInt(s[1]);

USACO Solutions for Java on their official website prefer BufferedReader and StringTokenizer instead of String.split() or Scanner.