Java 101Translating Scanner tokens into primitive types

time to read 1 min | 133 words

The Scanner class can be used to break an input into tokens separated by delimiters. Scanner also has methods to translate each token into one of Java’s primitive types.

Delimiters are string patterns and are set in the following way:

Scanner s = new Scanner(input);

If we need to read a file containing a set of integers, and calculate the total, we can use Scanner’s nextInt method:

int sum = 0;
//create a new instance of Scanner
Scanner s = new Scanner(
  new BufferedReader(
    new FileReader("in.txt")));
/*tokenize the input file and 
convert to integers*/
while (s.hasNext()) {
  if (s.hasNextInt()) {
    sum += s.nextInt();
  } else {;

In the example above, we iterate through the file, tokenizing each value and converting it to an integer before adding it to sum.