The static method below reads a specific given pattern from the console. If pattern is not ound, it raises InputMismatchException.

Java Code:
	public static String readPattern() throws InputMismatchException
	{
		Scanner in = new Scanner(System.in);  
		String str;
		System.out.println("Enter an integer value: ");
		str = in.next("[A-Za-z]*_");
		return str;
	}
We may call the above method as follows:

Java Code:
try{
System.out.println(readPattern());
}
catch(InputMismatchException ex)
{
System.out.println("Pattern not found.");	
}