The useDelimiter method for solving Scanner

Category: Java SE
 
waiter94
2011-10-03 10:40:57

Sponsored Links
import java.util. *;
import java.io. *;
import java.math. *;

public class Jicheng {
public static void main (String [] args) {
String ; input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner (input). useDelimiter ("\ \ s * fish \ \ s *");
System.out. println (s.nextInt ());
System.out.println (s.nextInt ());
System.out.println (s.next ());
System.out.println (s.next ());
s.close ();
} ;
}

unclear "\ \ s * fish \ \ s *" in \ \ s * meaning

Sponsored Links

zhangxiangxianggood
2011-10-03 10:56:58
\ \ s: A whitespace character: [\ t \ n \ x0B \ f \ r]
*: zero or more times
syxykjcjj
2011-10-03 11:04:00
\ \ s * regular expression type, which means that all spaces
csn710056577
2011-10-03 11:12:45
said it should be the exact whitespace
john1983
2011-10-03 11:23:13
Scanner.useDelimiter method accepts a regular expression instead of the ordinary String.

contents of the string is a regular expression, \ s *, will be written as a string literal inside the source code, you need to use double quotes to wrap while '\ 'character must also be escaped as "\ \", so it becomes "\ \ s *".
yefeng3107001360
2011-10-03 11:31:16
useDelimiter (Pattern pattern) This method is used to set the Scanner separator, by default scanner separator is a space, you this program is to use regular expressions to set the delimiter " \ \ s * fish \ \ s * "in front of a \ \ s * represents a space appear zero or more times and then appeared fish then appear 0 or more spaces, as long as the scanner scans encounter data are consistent with this regular expression , in front of it when a Scanner data can be used in the next () returns the data obtained.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.