如何在 Java Scanner 上“查看”下一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4288643/
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
How do I "peek" the next element on a Java Scanner?
提问by Juan Pablo Santos
That is, how do I get the next element of the iterator without removing it? As I may or may not want to remove it depending on its content. I have a file scanner where I iterate over XML tags using the Scanner next() method.
也就是说,如何在不删除它的情况下获取迭代器的下一个元素?因为我可能想也可能不想删除它,这取决于它的内容。我有一个文件扫描器,我在其中使用 Scanner next() 方法遍历 XML 标签。
Thanks in advance.
提前致谢。
采纳答案by Reese Moore
See thisanswer for a more efficient solution.
有关更有效的解决方案,请参阅此答案。
This is a very ugly solution, but you can create a wrapper class around Scanner
which keeps two internal Scanner
objects. You can get peek()
functionality by having the second scanner one ahead of the other
这是一个非常丑陋的解决方案,但您可以创建一个包装类来Scanner
保存两个内部Scanner
对象。您可以peek()
通过让第二台扫描仪先于另一台扫描仪来获得功能
This is a very basic solution (just to give you an idea of what I'm talking about) and doesn't implement all that you would need (but you would only need to implement those parts you would use). (also, this is untested, so take it with a grain of salt).
这是一个非常基本的解决方案(只是为了让您了解我在说什么)并且没有实现您需要的所有内容(但您只需要实现您将使用的那些部分)。(此外,这是未经测试的,因此请带上一粒盐)。
import java.util.Scanner;
public class PeekableScanner
{
private Scanner scan1;
private Scanner scan2;
private String next;
public PeekableScanner( String source )
{
scan1 = new Scanner(source);
scan2 = new Scanner(source);
next = scan2.next();
}
public boolean hasNext()
{
return scan1.hasNext();
}
public String next()
{
next = (scan2.hasNext() ? scan2.next() : null);
return scan1.next();
}
public String peek()
{
return next;
}
}
回答by DanielGibbs
I don't think there is a peek-like method, but you can use hasNext(String)to check if the next token is what you are looking for.
我不认为有类似 peek 的方法,但是您可以使用hasNext(String)来检查下一个令牌是否是您要查找的。
回答by Reese Moore
Here is another wrapper based solution, but this one has only one internal scanner. I left the other up to show one solution, this is a different, and probably better solution. Again, this solution doesn't implement everything (and is untested), but you will only have to implement those parts that you intend to use.
这是另一种基于包装器的解决方案,但这个解决方案只有一个内部扫描器。我留下了另一个来展示一个解决方案,这是一个不同的,可能是更好的解决方案。同样,此解决方案并未实现所有内容(并且未经测试),但您只需实现您打算使用的那些部分。
In this version you would keep around a reference to what the next()
actually is.
在这个版本中,您将保留对next()
实际内容的引用。
import java.util.Scanner;
public class PeekableScanner
{
private Scanner scan;
private String next;
public PeekableScanner( String source )
{
scan = new Scanner( source );
next = (scan.hasNext() ? scan.next() : null);
}
public boolean hasNext()
{
return (next != null);
}
public String next()
{
String current = next;
next = (scan.hasNext() ? scan.next() : null);
return current;
}
public String peek()
{
return next;
}
}
回答by popstr
There is a PeekingIterator in Google Guava: https://code.google.com/p/guava-libraries/wiki/CollectionHelpersExplained#PeekingIterator
Google Guava 中有一个 PeekingIterator:https: //code.google.com/p/guava-libraries/wiki/CollectionHelpersExplained#PeekingIterator
回答by Lluis Martinez
If the Iterator is a ListIterator you can do something like this:
如果 Iterator 是 ListIterator,您可以执行以下操作:
if(it.hasNext()) {
if(it.next() ...
it.previous();
}
回答by Dylanthepiguy
Here's a much simpler answer to all the others.
这是对所有其他人的更简单的答案。
We can use the match()
method to retrieve the last match of hasNext
, and look at the 0th capture group (which is the entire string matched by the regex --- exactly what we want).
我们可以使用该match()
方法检索 的最后一个匹配项hasNext
,并查看第 0 个捕获组(这是正则表达式匹配的整个字符串——正是我们想要的)。
Scanner s = ?new Scanner("a\nb")??????;
s.hasNext(".*"); // ".*" matches anything, similar to hasNext(), but updates the scanner's internal match variable
s.match().group(0)?;? // returns "a"
s.next() // returns "a"