扫描仪无法读取整个句子 - 扫描仪类的 next() 和 nextLine() 之间的区别
我正在编写一个程序,允许用户输入数据然后输出它。它的 3/4 正确,但当它到达输出地址时,它只打印一个单词,让我们只说“Archbishop Street”的“Archbishop”。我该如何解决这个问题?
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);
}
}
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?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
这种方法应该有效:
This approach should work:
以这种方式初始化扫描仪,以便它使用换行符分隔输入。
有关更多详细信息,请参阅 JavaDoc
。 next() 获取字符串中的整行
Initialize the Scanner this way so that it delimits input using a new line character.
Refer the JavaDoc for more details
Use sc.next() to get the whole line in a String
我会使用
Scanner#nextLine
与address
属性的next
相反。由于这会返回整行,并由行分隔符分隔,因此用户可以不受任何限制地输入任何地址。
I would use
Scanner#nextLine
opposed tonext
for youraddress
attribute.Since this returns the entire line, delimited by a line separator, it will allow the user to enter any address without any constraint.
Scanner 的默认分隔符是空格。检查 javadoc 了解如何更改此设置。
Default delimiter of Scanner is whitespace. Check javadoc for how to change this.
不要直接使用 System.in 和 System.out,而是使用
Console
类 - 它允许您显示提示并读取整行输入(从而解决您的问题)一通电话:Instead of using
System.in
andSystem.out
directly, use theConsole
class - it allows you to display a prompt and read an entire line (thereby fixing your problem) of input in one call:我们可以使用 scan.nextLine 轻松完成。它将读取输入的其余部分直到最后。然后将其分配给您的变量。整个句子可以轻松打印
。这是为了您更好地理解的示例。
}
}
We can easily done using scan.nextLine .It will read the rest of the input till the end. Then assign it to your variable. Entire sentence can be printed easily
. Here is the example for your better understanding.
}
}
我尝试了以下代码,但这并没有停止,因为没有中断条件,因此它总是等待输入,也许您可以添加中断条件
I tried following code but this is not stopping as there is no break condition so it always waiting for input , may be you could add a condition for break
next()
只会存储第一个标记之前的输入。如果有两个单词“Hi There”,则表示有两个由空格(分隔符)分隔的标记。每次调用 next() 方法时,它只会读取一个标记。如果输入是“你好!”
输出
,所以,如果您需要再次调用
next()
来获取下一个标记,这对于较长的字符串输入来说是低效的,而
nextLine()
会抓取用户输入的整行,即使有空格输出
next()
will only store the input up to the first token. if there are two words "Hi there" it means there are two tokens separated by space (delimiter). Every time you call the next() method it reads only one token.if input is "Hi there !"
Output
So if you both the words you need to call
next()
again to get the next token which is inefficient for longer string inputnextLine()
on the other hand grabs the entire line that the user enters even with spacesOutput
next()
只能读取输入直到空格。它无法读取由空格分隔的两个单词。此外,next()
在读取输入后将光标置于同一行。nextLine()
读取输入,包括单词之间的空格(即,它读取到行尾\n
)。读取输入后,nextLine()
将光标定位在下一行。要读取整行,您可以使用
nextLine()
next()
can read the input only till the space. It can't read two words separated by space. Also,next()
places the cursor in the same line after reading the input.nextLine()
reads input including space between the words (that is, it reads till the end of line\n
). Once the input is read,nextLine()
positions the cursor in the next line.for reading the entire line you can use
nextLine()
next()
读取空格之前的字符串。它在获得第一个空格后无法读取任何内容。nextLine()
读取整行。阅读直到行尾或“/n”。注意:不是下一行
(示例)
我的人生使命不仅仅是生存,而是蓬勃发展;
并且带着一些热情、一些同情心和一些幽默来做到这一点。
(输出)
我的
技巧:
如果您想阅读下一行,请检查 Java
has
方法。next()
reads the string before the space. it cannot read anything after it gets the first space.nextLine()
reads the whole line. Read until the end of the line or "/n".Note: Not The Next line
(Example)
My mission in life is not merely to survive, but to thrive;
and to do so with some passion, some compassion, some humor.
(Output)
My
My mission in life is not merely to survive, but to thrive;
Tricks:
If you want to read the next line Check Java
has
method.next() 会读取到遇到的空格,nextLine() 会读取到行尾。
扫描仪扫描 = new Scanner(System.in);
字符串地址 = scan.next();
s += scan.nextLine();
next() is read until the space of the encounter, and the nextLine() is read to the end of the line.
Scanner scan = new Scanner(System.in);
String address = scan.next();
s += scan.nextLine();
试试这个!
Try this !!!
要获取完整地址,请
在之前添加
To get the whole adress add
before
我将标记分隔符设置为换行模式,读取下一个标记,然后将分隔符放回原来的位置。
I set the token delimiter to be a newline pattern, read the next token, and then put the delimiter back to whatever it was.
“它只打印一个单词,让我们只说‘Archbishop Street’中的‘Archbishop’”
它只打印这个单词,因为扫描仪函数(例如 next()、nextInt() 等)每次只读取一个令牌。因此,该函数读取并返回下一个标记。
如果您想阅读整行“Archbishop Street”,请返回代码
"it only prints a word lets say only 'Archbishop' from 'Archbishop Street"
It is only printing the word because Scanner functions such as next(), nextInt(), etc. read only a token at time. Thus this function reads and returns the next token.
Going back to your code if you want to read the whole line "Archbishop Street"
下面是使用 Java Scanner 类读取标准输入中的一行的示例代码。
输入:
输出:
Below is the sample code to read a line in Standard input using Java Scanner class.
Input:
Output:
下面的代码应该可以满足 yoiu 的要求:
the below piece of code should do what yoiu are looking for:
String str = Scanner.next(); ===>它获取该行的第一个单词(hello world!! =>“hello”)
str +=scanner.nextLine(); ===>此方法返回当前行的其余部分,不包括末尾的任何行分隔符。 (你好世界!!=>“世界!!”)
String str = scanner.next(); ===> it fetch the 1st word of the line (hello world!! => "hello")
str += scanner.nextLine(); ===> This method returns the rest of the current line, excluding any line separator at the end. (hello world!! => " world!!")
我也有同样的问题。这应该适合你:
I had the same question. This should work for you:
使用
nextLine()
而不是next()
将句子作为字符串输入。Use
nextLine()
instead ofnext()
for taking sentence as string input.你可以这样做:
You can do this: