扫描仪 Java 混淆中的分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19818999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delimiter in Scanner Java confusion
提问by Aseem Bansal
According to Java API Scanner uses delimiters to break the whole input into tokens. I am trying to understand the tokens and delimiters. I was doing this program and hit a confusion
根据 Java API Scanner 使用分隔符将整个输入分解为标记。我试图理解标记和分隔符。我在做这个程序时遇到了困惑
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = null;
try {
s = new Scanner(System.in);
s.useDelimiter("A");
System.out.println("1 " + s.next().length());
System.out.println("2 " + s.next().length());
System.out.println("3 " + s.next().length());
System.out.println("4 " + s.next().length());
} finally {
if (s != null) {
s.close();
}
}
}
}
When I use the input AAAAAasdf
I get the following output.
当我使用输入时,AAAAAasdf
我得到以下输出。
1 0
2 0
3 0
4 0
I can understand this output as the length of tokens is zero between the delimiters hence all are zero but when I use the default delimiters and give the input as
我可以理解此输出,因为分隔符之间的标记长度为零,因此全部为零,但是当我使用默认分隔符并将输入作为
_____aaa\n
->Replace underscore by space and \n
by me hitting enter in eclipse console.
_____aaa\n
-> 用空格替换下划线,然后\n
在 Eclipse 控制台中按 Enter 键。
For this I am getting the output as
为此,我得到的输出为
1 3
which I cannot understand. I have given 5 spaces so there should be 4 tokens of 0 lengths between them. Why not? What am I missing here?
我无法理解。我给了 5 个空格,所以它们之间应该有 4 个长度为 0 的标记。为什么不?我在这里缺少什么?
采纳答案by Taylor
useDelimiter takes a regular expression pattern. The default pattern is
useDelimiter 采用正则表达式模式。默认模式是
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\p{javaWhitespace}+");
Which will match any amount of contiguous whitespace. If you want the delimiter to match any amount of contiguous A try something like
这将匹配任意数量的连续空白。如果您希望分隔符匹配任意数量的连续 A 尝试类似
s.useDelimiter("[A]+");
Read these: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#reset()
阅读这些:http: //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String) http://docs.oracle.com/javase/7 /docs/api/java/util/Scanner.html#reset()
回答by Ankit Rustagi
Its really interesting to see that when we specify " " (empty space) as a delimiter in the code
看到当我们在代码中指定“”(空格)作为分隔符时,真的很有趣
try {
s = new Scanner(System.in);
s.useDelimiter(" ");
System.out.println("1 " + s.next().length());
System.out.println("2 " + s.next().length());
System.out.println("3 " + s.next().length());
System.out.println("4 " + s.next().length());
} finally {
if (s != null) {
s.close();
}
}
and the input is
输入是
[5 spaces]asdf
we see the output
我们看到输出
1 0
2 0
3 0
4 0
But when we dont specify the delimiter,
但是当我们不指定分隔符时,
try {
s = new Scanner(System.in);
//s.useDelimiter(" ");
System.out.println("1 " + s.next().length());
System.out.println("2 " + s.next().length());
System.out.println("3 " + s.next().length());
System.out.println("4 " + s.next().length());
} finally {
if (s != null) {
s.close();
}
}
The same input
相同的输入
[5 spaces]asdf
generates a different output
产生不同的输出
1 4
So, i think specifying the delimiter, even though a default one makes the scanner skip all empty tokens.
因此,我认为指定分隔符,即使默认设置使扫描器跳过所有空标记。
回答by Sage
Scanner.next()
function Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern
. The default pattern is \\p{javaWhitespace}+
.
Scanner.next()
函数 从这个扫描器中查找并返回下一个完整的标记。一个完整的标记前后是与 匹配的输入delimiter pattern
。默认模式是\\p{javaWhitespace}+
。
To understand it better, try etting delimiter "\\s*"
:
为了更好地理解它,请尝试设置分隔符 "\\s*"
:
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\s*");
while(scanner.hasNext())
System.out.println(scanner.next());
For an input 123
, it scanner.next()
will print:
对于 input 123
,它将scanner.next()
打印:
1 // first println
2 //snd println
3 // third println
As X*
says that pattern X
can occur zeroor more times. This expression is known as Quantifiers. However an expression X+
says that X, one or more times
. So try using delimiter "[A]+"
which says that "A"
occurs one or more time, and matches any amount of contiguous "A"
如前所述X*
,该模式X
可以出现零次或多次。此表达式称为量词。然而,一个表达是这样X+
说的X, one or more times
。所以尝试使用分隔符"[A]+"
,它表示"A"
发生一次或多次,并匹配任意数量的连续"A"