JavaScanner Class with Delimiters Example in Java

Scanner Class with Delimiters Example in Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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:

 

[root@mypc]# java DelimiterInScanner
1
ABC Corp.
$55

Read more Java programming tutorials.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories