Java 如何使用 Scanner 类中的 hasNext()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34355062/
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 to use hasNext() from the Scanner class?
提问by Harshit Juneja
Input Format
输入格式
Read some unknown n lines of input from stdin(System.in)
until you reach EOF; each line of input contains a non-empty String.
读取一些未知的 n 行输入,stdin(System.in)
直到到达 EOF;每行输入包含一个非空字符串。
Output Format
输出格式
For each line, print the line number, followed by a single space, and then the line content received as input:
对于每一行,打印行号,后跟一个空格,然后是作为输入接收的行内容:
Sample Output
样本输出
Hello world
I am a file
Read me until end-of-file.
Here is my solution. The problem being I am not able to proceed till EOF. But the output is just:
这是我的解决方案。问题是我无法继续到 EOF。但输出只是:
Hello world
Here is my code:
这是我的代码:
public class Solution {
public static void main(String[] args) {
check(1); // call check method
}
static void check(int count) {
Scanner s = new Scanner(System.in);
if(s.hasNext() == true) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
check(count);
}
}
}
采纳答案by Willi Mentzel
Your code does not work because you create a new Scanner
object in every recursive call.
You should not use recursion for this anyways, do it iteratively instead.
您的代码不起作用,因为您Scanner
在每次递归调用中都创建了一个新对象。无论如何,您不应该为此使用递归,而是迭代地进行。
Iterative version
迭代版本
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int count = 1;
while(s.hasNext()) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
}
}
}
Recursive version
递归版本
public class Solution {
private Scanner s;
public static void main(String[] args) {
s = new Scanner(System.in); // initialize only once
check(1);
}
public static void check(int count) {
if(s.hasNext()) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
check(count + 1);
}
}
}
回答by Suresh Atta
Change
改变
if (s.hasNext() == true) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
System.out.print(count);
check(count);
}
to:
到:
while (s.hasNext()) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
System.out.print(count);
check(count);
}
while
loops continues until the data exists, where as if
checks for only once.
while
循环继续直到数据存在,其中 asif
只检查一次。
回答by Ian2thedv
If using recursion is a requirement, you can use a helper function:
如果需要使用递归,则可以使用辅助函数:
static void check(int count) {
Scanner s = new Scanner(System.in);
check(count, s);
}
static void check(int count, Scanner scanner) {
if(!scanner.hasNext()) {
return;
}
String ns = scanner.nextLine();
System.out.println(count + " " + ns);
check(++count, scanner);
}
Notice how new Scanner(System.in)
is only called once.
注意 hownew Scanner(System.in)
只被调用一次。
回答by Ramesh-X
Scanner
is kind of a BufferedReader
(I'm not telling about inheritance or something. I'm telling they both have buffers. Scanner
has just a small one). So after you enter text in the Console, those are read()
from System.in
and stored in the buffer inside the Scanner
.
Scanner
是一种BufferedReader
(我不是在谈论继承或其他东西。我是说他们都有缓冲区。Scanner
只有一个小缓冲区)。因此,在控制台中输入文本后,这些文本read()
来自System.in
并存储在Scanner
.
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
s1.hasNext();
Scanner s2 = new Scanner(System.in);
while(true){
System.out.println("Read line:: " + s2.nextLine());
}
}
Use the following input to the Scanner
:
使用以下输入Scanner
:
line 1
line 2
line 3
line 4
线 1
线 2
线 3
线 4
You will get the output:
你会得到输出:
Read line:: e 1
Read line:: line 2
Read line:: line 3
Read line:: line 4
读取行:: e 1
读取行:: 行2
读取行:: 行3
读取行:: 行4
I think you might know the reason to this output. Some characters of the first line are in the Scanner s1
. Therefore don'tcreate 2 Scanner
s to take input from same Stream
.
我想你可能知道这个输出的原因。第一行的一些字符在Scanner s1
. 因此,不要创建 2 Scanner
s 来从相同的Stream
.
You can change your code as follows to get required output.
您可以按如下方式更改代码以获得所需的输出。
private static Scanner s;
public static void main(String[] args) {
s = new Scanner(System.in);
check(1); // call check method
}
static void check(int count) {
if (s.hasNextLine()) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
check(count);
}
}
You can use s.hasNextLine()
instead of s.hasNext()
as you are reading line by line.
您可以在逐行阅读时使用s.hasNextLine()
代替s.hasNext()
。
No need to use s.hasNextLine()==true
as that statement will be true
if and only if s.hasNextLine()
is true
.
无需使用,s.hasNextLine()==true
因为该语句将是true
当且仅当s.hasNextLine()
是true
。
You can give EOF
character to the console using Ctrl+Z
in Windows system and Ctrl+D
in Unix. As I know, you can't send EOF
character using the output window of NetBeans.
您可以在 Windows 系统和Unix 中EOF
为控制台提供字符。据我所知,您不能使用 NetBeans 的输出窗口发送字符。Ctrl+Z
Ctrl+D
EOF
回答by user3480296
public static void main(String[] args) {
List<String> eol = new ArrayList<String>();
Scanner in=new Scanner(System.in);
int t=1;
while(in.hasNext()){
eol.add(in.nextLine());
}
for (String i:eol){
System.out.println(t+" "+i);
t++;
}
}
回答by myer
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 1;
while(scan.hasNext()){
System.out.println(count++ + " " + scan.nextLine());
}
}
回答by JeyaRama Balan Rajagani
Here is the error free program.
这是无错误程序。
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 1;
while(scan.hasNext()) {
String s = scan.nextLine();
System.out.println(count + " " + s);
count++;
}
}
}
回答by worked
Scanner scanner = new Scanner(System.in);
int i = 1;
while(scanner.hasNext()) {
System.out.println(i + " " + scanner.nextLine());
i++;
}
回答by divya
Use while instead of if,but recursive version is better.Here is iterative version:
使用while代替if,但递归版本更好。这里是迭代版本:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
{
int i=0;
while(sc.hasNext()==true)
{
i=i+1;
String s=sc.nextLine();
System.out.println(i+" "+s);
};
}
}
}
回答by Hetal Rachh
java.util.Scanner.hasNext()
basically helps you to read the input. This method returns true if this Scanner has another token of any type in its input. Returns false otherwise.
Check below code snippet,
java.util.Scanner.hasNext()
基本上可以帮助您阅读输入。如果此 Scanner 在其输入中有任何类型的另一个标记,则此方法返回 true。否则返回 false。检查下面的代码片段,
while(sc.hasNext()) {
System.out.println(i + " " + sc.nextLine());
i++;
}
You can find complete code at below link,
您可以在下面的链接中找到完整的代码,
https://github.com/hetalrachh/HackerRank/blob/master/Practice/Java/Introduction/EndOfFile.java
https://github.com/hetalrachh/HackerRank/blob/master/Practice/Java/Introduction/EndOfFile.java