Skip to content

Do I scan twice if I call scanner.hasNext and then

Do I scan twice if I call scanner.hasNext(pattern) and then with the same pattern on java.util.Scanner

Let’s say i have this code with a lots of cases (trying to make a lexer):

import java.util.*;
import java.util.regex.Pattern;

public class MainClass {
   public static void main(String[] args) {
      Scanner scanner = new Scanner("Hello World! 3 + 3.0 = 6 ");

      Pattern a = Pattern.compile("..rld!");
      Pattern b = Pattern.compile("...llo");

      while(scanner.hasNext()) {
         if (scanner.hasNext(a)) {
            /*Do something meaningful with it like create a token*/
         else if(scanner.hasNext(b)) {

      // close the scanner

My questions are:

  • Does the hasNext(pattern) caches somehow the result of the search? So it doesn’t search the same pattern twice
  • Is this slower or faster than using try { } catch { ... }
  • Or is there an easier way (without third-party libraries) to tokenize based on the regex patterns



Ok so I think that the answer is:

Documentation doesn’t say anything so it may be possible, but it probably does not.

Also, I was primarily asking because i wanted to use it for parsing more complex things like string literals and not just white space separated tokens. And found out that Scanner still takes such token and then it checks if it matches. So it is now useless for my use case.

5 People found this is helpful