The java.util.Scanner for Parsing Text in Java


The Scanner class is not only useful for reading input from the console but also for parsing text and strings with custom delimiters.

Source Code

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\s*fish\s*");
System.out.println(s.nextInt()); // Outputs 1
System.out.println(s.nextInt()); // Outputs 2
s.close();
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments