Java 扫描仪不读取整个句子 - 扫描仪类的 next() 和 nextLine() 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4058912/
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
Scanner doesn't read whole sentence - difference between next() and nextLine() of scanner class
提问by Bonett09
I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from 'Archbishop Street'. How do I fix this?
我正在编写一个程序,允许用户输入他的数据然后输出它。它的 3/4 正确,但是当它到达输出地址时,它只打印一个单词,让我们只说“大主教街”中的“大主教”。我该如何解决?
import java.util.*;
class MyStudentDetails{
public static void main (String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter Your Name: ");
String name = s.next();
System.out.println("Enter Your Age: ");
int age = s.nextInt();
System.out.println("Enter Your E-mail: ");
String email = s.next();
System.out.println("Enter Your Address: ");
String address = s.next();
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("E-mail: "+email);
System.out.println("Address: "+address);
}
}
回答by Raghuram
回答by Michael Borgwardt
Instead of using System.in
and System.out
directly, use the Console
class - it allows you to display a prompt and read an entire line (thereby fixing your problem) of input in one call:
不是直接使用System.in
and System.out
,而是使用Console
该类 - 它允许您在一次调用中显示提示并读取整行输入(从而解决您的问题):
String address = System.console().readLine("Enter your Address: ");
回答by Anthony Forloney
I would use Scanner#nextLine
opposed to next
for your address
attribute.
我会用Scanner#nextLine
反对next
你的address
属性。
This method returns the rest of the current line, excluding any line separator at the end.
此方法返回当前行的其余部分,不包括末尾的任何行分隔符。
Since this returns the entire line, delimited by a line separator, it will allow the user to enter any address without any constraint.
由于这将返回由行分隔符分隔的整行,因此它将允许用户无限制地输入任何地址。
回答by user114573
You can do this:
你可以这样做:
class MyStudentDetails{
public static void main (String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter Your Name: ");
String name = s.nextLine();
System.out.println("Enter Your Age: ");
String age = s.nextLine();
System.out.println("Enter Your E-mail: ");
String email = s.nextLine();
System.out.println("Enter Your Address: ");
String address = s.nextLine();
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("E-mail: "+email);
System.out.println("Address: "+address);
}
}
回答by Nomad
回答by Eugene Prokopenko
I had the same question. This should work for you:
我有同样的问题。这应该适合你:
s.nextLine();
回答by Arvind Ramachandran
This approach is working, but I don't how, can anyone explain, how does it works..
这种方法有效,但我不知道它是如何工作的,谁能解释一下。
String s = sc.next();
s += sc.nextLine();
回答by Akshay Suresh
Use nextLine()
instead of next()
for taking sentence as string input.
使用nextLine()
,而不是next()
采取一句字符串输入。
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
回答by Bharathi Mohan
To get the whole adress add
要获取整个地址添加
s.nextLine();//read the rest of the line as input
before
前
System.out.println("Enter Your Address: ");
String address = s.next();
回答by Vinayak
String s="Hi";
String s1="";
//For Reading Line by hasNext() of scanner
while(scan.hasNext()){
s1 = scan.nextLine();
}
System.out.println(s+s1);
/*This Worked Fine for me for reading Entire Line using Scanner*/