Java 扫描仪没有';t阅读整个句子-scanner类的next()和nextLine()之间的差异
我正在编写一个程序,允许用户输入数据,然后输出数据。它的3/4是正确的,但是当它到达输出地址时,它只打印一个单词,比如说“大主教街”中的“大主教”。我该如何解决这个问题Java 扫描仪没有';t阅读整个句子-scanner类的next()和nextLine()之间的差异,java,java.util.scanner,Java,Java.util.scanner,我正在编写一个程序,允许用户输入数据,然后输出数据。它的3/4是正确的,但是当它到达输出地址时,它只打印一个单词,比如说“大主教街”中的“大主教”。我该如何解决这个问题 import java.util.*; class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println("Ent
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);
}
}
扫描仪的默认分隔符为空白。检查如何更改此选项。不要使用
系统。在
和系统中,直接使用该类-它允许您在一次调用中显示提示并读取整行输入(从而解决您的问题):
String address = System.console().readLine("Enter your Address: ");
我会使用反对next
作为您的address
属性
此方法返回当前行的其余部分,不包括末尾的任何行分隔符
由于这将返回由行分隔符分隔的整行,因此允许用户输入任何地址而不受任何限制。您可以执行以下操作:
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);
}
}
以这种方式初始化扫描仪,以便它使用新行字符分隔输入
Scanner sc = new Scanner(System.in).useDelimiter("\\n");
有关更多详细信息,请参阅
使用sc.next()将整行字符串化我有同样的问题。这应该适合您:
s.nextLine();
这种方法是有效的,但我不知道,谁能解释一下,它是如何工作的
String s = sc.next();
s += sc.nextLine();
使用nextLine()
代替next()
将句子作为字符串输入
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
要获得整个地址,请添加
s.nextLine();//read the rest of the line as input
以前
System.out.println("Enter Your Address: ");
String address = s.next();
我将标记分隔符设置为换行符模式,读取下一个标记,然后将分隔符放回原来的位置
public static String readLine(Scanner scanner) {
Pattern oldDelimiter = scanner.delimiter();
scanner.useDelimiter("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]");
String r = scanner.next();
scanner.useDelimiter(oldDelimiter);
return r;
}
next()
只能读取输入直到空格。它不能读两个被空格隔开的单词。另外,next()
在读取输入后将光标放在同一行中
nextLine()。读取输入后,nextLine()
将光标定位在下一行
为了读取整行,您可以使用nextLine()
我尝试了以下代码,但这并没有停止,因为没有中断条件,所以它总是等待输入,可能是您可以添加中断条件
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s="";
while(scan.hasNext())
{
s=scan.nextLine();
}
System.out.println("String: " +s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
下面是使用Java Scanner类读取标准输入中的一行的示例代码
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(s);
scan.close();
}
}
输入:
你好,世界
输出:
你好,世界
“它只打印一个单词,比如说‘大主教街’中的‘大主教’
它只是打印单词,因为诸如next()、nextInt()等扫描器函数一次只读取一个标记。因此,此函数读取并返回下一个令牌
For example if you have: x y z
s.next() will return x
s.nextLine() y z
如果你想读整行“大主教街”的话,回到你的代码
我们可以很容易地使用scan.nextLine完成。它将读取其余的输入直到结束。然后将其分配给变量。整个句子可以很容易地打印出来
. 下面是一个让你更好理解的例子
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
String s2;
scan.nextLine(); // read the rest of the line of input (newline character after the double token).
s2 = scan.nextLine();
/* Concatenate and print the String variables on a new line integer variables on a new line;
System.out.println(s + s2);
scan.close();
}
}以下代码应该满足yoiu的要求:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(s);
scan.close();
}
java.util.Scanner;util-package,Scanner-Class
next()
读取空格前的字符串。它在获得第一个空格后无法读取任何内容
nextLine()
读取整行内容。一直读到行尾或“/n”。
注意:不是下一行
(示例)
我的人生使命不仅仅是生存,而是茁壮成长
带着一些激情,一些同情,一些幽默
(输出)
我的
我的人生使命不仅仅是生存,而是茁壮成长
技巧:
如果您想阅读下一行,请检查Javahas
方法
while (scanner.hasNext()) {
scan.next();
}
while (scanner.hasNext()) {
scan.nextLine();
}
next()
将只存储第一个令牌之前的输入。如果有两个单词“Hi there”,则表示有两个由空格(分隔符)分隔的标记。每次调用next()方法时,它只读取一个标记
如果输入为“你好!”
Scanner scan=新的扫描仪(System.in);
String str=scan.next()
系统输出打印项次(str);
输出
嗨
因此,如果您同时输入两个单词,则需要再次调用next()
,以获取下一个标记,这对于较长的字符串输入来说效率很低
另一方面,nextLine()
会抓住用户输入的整行内容,即使有空格
Scanner scan=新的扫描仪(System.in);
String str=scan.nextLine()
系统输出打印项次(str);
输出
你好
next()一直读取到相遇的空格,nextLine()一直读取到行的末尾。
Scanner scan=新的扫描仪(System.in)代码>
String address=scan.next()代码>
s+=scan.nextLine()代码>
字符串str=scanner.next();=>它获取行的第一个单词(hello world!!=>“hello”)
str+=scanner.nextLine();=>此方法返回当前行的其余部分,不包括末尾的任何行分隔符。(hello world!!=>“world!!”还有其他简单的方法吗,因为我还没有了解Console类?@Bonett09:现在你已经了解了。在某些方面,它实际上比直接使用输入和输出流更简单。这个答案是错误的。如果测试此代码,则带有“s.nextLine();”的行将不返回任何内容—一个空字符串。不要用这个答案@Rubinum这里的代码没有错(逻辑上是错的,但技术上不是错的),如果代码返回空字符串,那么您就无法正确使用s.nextLine()
。这不是user114573s的错误。因为方法nextLine()
在游标当前位置之后从当前行读取下一个符号。例如,如果您的输入包含两行1
和Hello World
。源代码如下:intx=sc.nextInt();字符串s=sc.nextLine()
。在这种情况下,在nextInt()
之后,光标停留在符号“1”之后
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
String s2;
scan.nextLine(); // read the rest of the line of input (newline character after the double token).
s2 = scan.nextLine();
/* Concatenate and print the String variables on a new line integer variables on a new line;
System.out.println(s + s2);
scan.close();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println(s);
scan.close();
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
s += scan.nextLine();
System.out.println("String: " + s);
}
}
while (scanner.hasNext()) {
scan.next();
}
while (scanner.hasNext()) {
scan.nextLine();
}
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
str += scanner.readLine();