JavaScanner Class with Delimiters Example in Java

Scanner Class with Delimiters Example in Java

Java Developer Tutorials

Scanner is an important class for accepting input in Java. In the sample code below, we seek to illustrate how to use delimiters when scanning input. This method can be used in predefined cases where you know the data and delimiters that you are getting.

*/

import java.util.Scanner;

public class DelimiterInScanner{
	
	public static void main(String[] args) 
	{  
		DelimiterInScanner delimiterInScanner = new DelimiterInScanner();
		delimiterInScanner.proceed();
	}

	public void proceed()
	{
		String deLimiterString = "deLimiter" ;
		String inputString = "1 deLimiter ABC Corp. deLimiter $55 deLimiter";
		
		Scanner scanner = new Scanner(inputString).useDelimiter("\\s*"+deLimiterString+"\\s*");
		
		System.out.println(scanner.nextInt());
		System.out.println(scanner.next());
		System.out.println(scanner.next());
		
		scanner.close(); 
	}
		
}

/*

Here is the output you can expect when running this code:

 

[[email protected]]# java DelimiterInScanner
1
ABC Corp.
$55

Read more Java programming tutorials.

Latest Posts

Related Stories