java中Scanner的hasNext()的疑问
2016-06-06 14:20
281 查看
请问下面代码有什么区别,该如何输入才能使第一段代码不满足条件而跳出循环?
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
System.out.println(s.next());
}
第一个问题,两段代码的区别在于阻塞的位置不同,加上一行输出代码就可以很明显地看到差别。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
System.out.print("You inputted: ");
System.out.println(s.next());
}
}
}
hasNext()方法会阻塞,不代表next()方法就不会阻塞,我们看源码。
/**
* Returns true if this scanner has another token in its input.
* This method may block while waiting for input to scan.
* The scanner does not advance past any input.
*
* @return true if and only if this scanner has another token
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}
第二个问题,想要结束循环,在Windows环境下,需要输入Ctrl+Z;而在Unix环境下,需要输入Ctrl+D(在myeclipse编译器中也是如此操作)。注意,这是输入,而不是对控制台进行操作。这相当于向控制台输入一个字符,这个字符代表EOF,此时hasNext()方法返回false,循环结束。
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
System.out.println(s.next());
}
Scanner s = new Scanner(System.in); while(true) { System.out.println(s.next()); }
第一个问题,两段代码的区别在于阻塞的位置不同,加上一行输出代码就可以很明显地看到差别。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
System.out.print("You inputted: ");
System.out.println(s.next());
}
}
}
hasNext()方法会阻塞,不代表next()方法就不会阻塞,我们看源码。
/**
* Returns true if this scanner has another token in its input.
* This method may block while waiting for input to scan.
* The scanner does not advance past any input.
*
* @return true if and only if this scanner has another token
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}
/** * Finds and returns the next complete token from this scanner. * A complete token is preceded and followed by input that matches * the delimiter pattern. This method may block while waiting for input * to scan, even if a previous invocation of {@link #hasNext} returned * <code>true</code>. * * @return the next token * @throws NoSuchElementException if no more tokens are available * @throws IllegalStateException if this scanner is closed * @see java.util.Iterator */ public String next() { ensureOpen(); clearCache while (true) { String token = getCompleteTokenInBuffer(null); if (token != null) { matchValid = true; skipped = false; return token; } if (needInput) readInput(); else throwFor(); } }
第二个问题,想要结束循环,在Windows环境下,需要输入Ctrl+Z;而在Unix环境下,需要输入Ctrl+D(在myeclipse编译器中也是如此操作)。注意,这是输入,而不是对控制台进行操作。这相当于向控制台输入一个字符,这个字符代表EOF,此时hasNext()方法返回false,循环结束。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树