In this section of my series of java tutorial we will be showing on how to tokenize string using Scanner class. Scanner class can take an argument String variable into its constructor. by calling the static method useDelimeter() of the scanner class, you can now set the value of delimiter. In this sample java source we will be using the dash “-” character as our delimiter.

package com.javatutorialhq.tutorial;

import java.util.Scanner;

public class TokenizeUsingScanner {

	 * This java sample code shows how to split
	 * String value into tokens using
	 * Scanner. This program tokenize
	 * the input string base on the delimiter
	 * set by calling the useDelimiter method
	public static void main(String[] args) {
		String input = "01-02-03-04656-TEST";
		Scanner s = new Scanner(input);

Running this java source code (tokenize string using Scanner) will give you the following output:


The input string is 01-02-03-04656-TEST which is then passed to the scanner constructor. And then as we iterate through the tokens, we are printing the same thus the result shown above.

Suggested Reading List