sc = new Scanner(new File(dataFile));
sc.useDelimiter(",|\r\n");
I don't understand how delimiter works, can someone explain this in layman terms?
我不明白分隔符是如何工作的,有人可以用外行的方式解释这个吗?
3 个解决方案
#1
65
The scanner can also use delimiters other than whitespace.
扫描仪还可以使用除空白之外的分隔符。
Easy example from Scanner API:
Scanner API的简单示例:
String input = "1 fish 2 fish red fish blue fish";
// \\s* means 0 or more repetitions of any whitespace character
// fish is the pattern to find
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt()); // prints: 1
System.out.println(s.nextInt()); // prints: 2
System.out.println(s.next()); // prints: red
System.out.println(s.next()); // prints: blue
// don't forget to close the scanner!!
s.close();
The point is to understand the regular expressions (regex
) inside the Scanner::useDelimiter
. Find an useDelimiter
tutorial here.
关键是要理解Scanner :: useDelimiter中的正则表达式(正则表达式)。在这里找到useDelimiter教程。
To start with regular expressions here you can find a nice tutorial.
从这里开始使用正则表达式,您可以找到一个很好的教程。
Notes
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
#2
7
With Scanner the default delimiters are the whitespace characters.
使用Scanner,默认分隔符是空白字符。
But Scanner can define where a token starts and ends based on a set of delimiter, wich could be specified in two ways:
但是,Scanner可以根据一组分隔符定义令牌的开始和结束位置,可以通过两种方式指定:
- Using the Scanner method: useDelimiter(String pattern)
- Using the Scanner method : useDelimiter(Pattern pattern) where Pattern is a regular expression that specifies the delimiter set.
使用Scanner方法:useDelimiter(String pattern)
使用Scanner方法:useDelimiter(Pattern pattern)其中Pattern是指定分隔符集的正则表达式。
So useDelimiter()
methods are used to tokenize the Scanner input, and behave like StringTokenizer class, take a look at these tutorials for further information:
因此,useDelimiter()方法用于标记Scanner输入,并且行为类似于StringTokenizer类,请查看这些教程以获取更多信息:
And here is an Example:
这是一个例子:
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("Anna Mills/Female/18");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the tokenized Strings
while(scan.hasNext()){
System.out.println(scan.next());
}
// closing the scanner stream
scan.close();
}
Prints this output:
打印此输出:
Anna Mills
Female
18
#3
3
For example:
String myInput = null;
Scanner myscan = new Scanner(System.in).useDelimiter("\\n");
System.out.println("Enter your input: ");
myInput = myscan.next();
System.out.println(myInput);
This will let you use Enter as a delimiter.
这将允许您使用Enter作为分隔符。
Thus, if you input:
因此,如果您输入:
Hello world (ENTER)
it will print 'Hello World'.
它将打印'Hello World'。
#1
65
The scanner can also use delimiters other than whitespace.
扫描仪还可以使用除空白之外的分隔符。
Easy example from Scanner API:
Scanner API的简单示例:
String input = "1 fish 2 fish red fish blue fish";
// \\s* means 0 or more repetitions of any whitespace character
// fish is the pattern to find
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt()); // prints: 1
System.out.println(s.nextInt()); // prints: 2
System.out.println(s.next()); // prints: red
System.out.println(s.next()); // prints: blue
// don't forget to close the scanner!!
s.close();
The point is to understand the regular expressions (regex
) inside the Scanner::useDelimiter
. Find an useDelimiter
tutorial here.
关键是要理解Scanner :: useDelimiter中的正则表达式(正则表达式)。在这里找到useDelimiter教程。
To start with regular expressions here you can find a nice tutorial.
从这里开始使用正则表达式,您可以找到一个很好的教程。
Notes
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
#2
7
With Scanner the default delimiters are the whitespace characters.
使用Scanner,默认分隔符是空白字符。
But Scanner can define where a token starts and ends based on a set of delimiter, wich could be specified in two ways:
但是,Scanner可以根据一组分隔符定义令牌的开始和结束位置,可以通过两种方式指定:
- Using the Scanner method: useDelimiter(String pattern)
- Using the Scanner method : useDelimiter(Pattern pattern) where Pattern is a regular expression that specifies the delimiter set.
使用Scanner方法:useDelimiter(String pattern)
使用Scanner方法:useDelimiter(Pattern pattern)其中Pattern是指定分隔符集的正则表达式。
So useDelimiter()
methods are used to tokenize the Scanner input, and behave like StringTokenizer class, take a look at these tutorials for further information:
因此,useDelimiter()方法用于标记Scanner输入,并且行为类似于StringTokenizer类,请查看这些教程以获取更多信息:
And here is an Example:
这是一个例子:
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("Anna Mills/Female/18");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the tokenized Strings
while(scan.hasNext()){
System.out.println(scan.next());
}
// closing the scanner stream
scan.close();
}
Prints this output:
打印此输出:
Anna Mills
Female
18
#3
3
For example:
String myInput = null;
Scanner myscan = new Scanner(System.in).useDelimiter("\\n");
System.out.println("Enter your input: ");
myInput = myscan.next();
System.out.println(myInput);
This will let you use Enter as a delimiter.
这将允许您使用Enter作为分隔符。
Thus, if you input:
因此,如果您输入:
Hello world (ENTER)
it will print 'Hello World'.
它将打印'Hello World'。