Use Scanner to search in lines in Java
Description
The following code shows how to use Scanner to search in lines.
Example
/* w ww. j a v a 2 s . c om*/
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
String instr = "Name: Tom Age: 28 ID: 77";
Scanner conin = new Scanner(instr);
conin.findInLine("Age:");
if (conin.hasNext())
System.out.println(conin.next());
else
System.out.println("Error!");
}
}
The code above generates the following result.