我使用扫描器方法nextInt()和nextLine()读取输入。
它看起来是这样的:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
问题是,在输入数值之后,第一个输入。nextline()被跳过,第二个输入。nextline()被执行,所以我的输出看起来像这样:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string // ...and this line is executed and waits for my input
我测试了我的应用程序,看起来问题在于使用input。nextInt()。如果我删除它,那么string1 = input。nextLine()和string2 = input。nextLine()都将按照我想要的方式执行。
# # #这是因为Scanner.nextInt
method does not read the方法不会读取由按“Enter”创建的输入中的换行符,因此调用toScanner.nextLine
returns after reading that newline .
当您使用Scanner时,您将遇到类似的行为。nextLine在Scanner。next()或任何Scanner之后。nextFoo方法(除了nextLine本身)。
处理:
要么放一个扫描器。在每个Scanner之后调用nextLine。nextInt或扫描仪。nextFoo来消耗包括换行符在内的那一行的其余部分
int option = input.nextInt();
input.nextLine(); // Consume newline left-over
String str1 = input.nextLine();
或者通过Scanner读取输入更好。nextLine并将您的输入转换为所需的适当格式。例如,你可以使用integer 。 parseint (String)方法转换为整数。
int option = 0;
try {
option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
String str1 = input.nextLine();
问题是input。nextInt()方法——它只读取int值。因此,当您继续使用input。nextLine()进行读取时,您会收到“n”回车键。因此,要跳过这一步,必须添加input。nextLine()。希望现在大家都明白了。
试试这样:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
###这是因为当你输入一个数字,然后按<kbd> enter </kbd>input.nextInt()
consumes only the number not the "end of line". When input.nextL只消耗数字而不是“行尾”。当
input.nextLine()
executes it consumes the "end of line" still in the b执行它将消耗第一个输入的缓冲区中仍然存在的“行尾”。
应该在input。nextInt()之后立即使用input。nextLine()
关于这个问题似乎有很多问题java.util.Scanner
. I think a more readable/idiomatic solution would be to cal. 我认为一个更可读/习惯的解决方案是调用scanner.skip("[\r\n]+")
to drop any newline characters after calling nextInt()<在调用后删除任何换行符
nextInt()
.
EDIT:正如@PatrickParker下面提到的,如果用户在数字后输入任何空格,将导致一个无限循环。他们给出了一个更好的与skip一起使用的模式的答案:https://stackoverflow。com/a/42471816/143585
###它这样做是因为input.nextInt();
doesn't capture the newlin不捕获换行符。你可以像其他人建议的那样添加一个input.nextLine();
underneath.
下面。< br >
或者你也可以用c#的风格来做,将nextLine解析为一个整数,像这样:
int number = Integer.parseInt(input.nextLine());
这样做同样有效,并且可以节省一行代码。
# # # TL;博士< br / >
使用scanner.skip("\\R?")
bef之前scanner.nextLine()
if you 如果你不确定之前的指示是否scanner.next()
or scanner.next*TypeName*()
.
scanner.next*TypeName*()
.
Skip使用正则表达式
\R表示行分隔符
? 将使\R行分隔符序列可选-将防止跳过方法从
(a)等待匹配序列——如系统中仍开放数据源。,但是没有找到行分隔符
(b)扔java.util.NoSuchElementException
in case of t在终止/结束数据源(如文件或字符串)的情况下
你需要知道的事情:
表示很少行的文本也包含不可打印的行间字符(我们称之为行分隔符),比如
回车(CR -在字符串字面值表示为“\r”)
换行(LF -在字符串字面值表示为“\n”)
当您从控制台读取数据时,它允许用户输入他的响应,当他完成时,他需要以某种方式确认这一事实。要做到这一点,用户必须按键盘上的“输入”/“返回”键。
重要的是,这个键除了确保将用户数据放置到标准输入(由System。在其中是由扫描器读取)也发送操作系统依赖的行分隔符(如Windows \r\n)后。
因此,当你询问用户的值,比如年龄,用户输入42并按回车键时,标准输入将包含“42\r\n”。
问题
扫描器#nextInt(和其他扫描器#nextType方法)不允许扫描器使用这些行分隔符。它将从系统中读取它们。in(否则Scanner怎么知道代表用户年龄值的数字不会比面对的空格多?)将从标准输入中删除这些数字,但它也会在内部缓存这些行分隔符。我们需要记住的是,所有的Scanner方法总是从缓存的文本开始扫描。
现在扫描器#nextLine()只是收集并返回所有字符,直到它找到行分隔符(或流的结束)。但是,由于在从控制台读取数字后,行分隔符会立即在Scanner的缓存中找到,它将返回空字符串,这意味着Scanner无法在这些行分隔符(或流结束)之前找到任何字符。
顺便说一句nextLine
als也消耗那些行分隔符。
解决方案
所以当你想要问数字,然后为整个行同时避免空字符串作为nextLine的结果
消耗留下的行分隔符nextInt
from Scanners cache by从扫描仪缓存
调用nextLine
或者在我看来更可读的方式是通过调用skip("\\R")
or skip("\r\n|\r|\n")
或skip("\r\n|\r|\n")
to 让扫描器跳过行分隔符匹配的部分(更多信息关于\R
: https://stackoverflow.com/a/31060125): https://stackoverflow。com/a/31060125)
不要使用nextInt
(nor nextTYPE
methods方法)。相反,逐行读取整个数据使用nextLine
and parse numbers from each line (assuming one line contains 并从每一行(假设一行只包含一个数字)解析数字到合适的类型,如int
via Integer.parseInt
.通过Integer.parseInt
..
顺便说一下:扫描器#nextType方法可以跳过分隔符(默认情况下,所有的空白空间,如制表符行分隔符),包括扫描器缓存的那些,直到它们找到下一个非分隔符值(令牌)。感谢输入像“42\r\n\r\n321\r\n\r\n\r\nfoobar”的代码
int num1 = sc.nextInt();
int num2 = sc.nextInt();
String name = sc.next();
将能够正确分配num1=42 num2=321 name=foobar。
# # #代替input.nextLine()
use input.使用
input.next()
that这应该能解决问题。
修改代码:
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Insert a number: ");
int number = input.nextInt();
System.out.print("Text1: ");
String text1 = input.next();
System.out.print("Text2: ");
String text2 = input.next();
}
###如果你想同时读取字符串和整数,一个解决方案是使用两个扫描仪:
Scanner stringScanner = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);
intScanner.nextInt();
String s = stringScanner.nextLine(); // unaffected by previous nextInt()
System.out.println(s);
intScanner.close();
stringScanner.close();
###为了避免使用问题nextLine();
immediately after nextInt();nextInt();
as it helps in cle因为它有助于清除缓冲区。当你按下ENTER
the nextInt();
does not capture the new li的nextInt();
does没有捕获新的行,因此跳过Scanner
code later.
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
scanner.nextLine(); //clearing the buffer
###如果你想快速扫描输入而不被扫描器类所迷惑,nextLine()方法使用自定义的输入扫描器。
代码:
class ScanReader {
/**
* @author Nikunj Khokhar
*/
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) return -1;
}
return buf[index++];
}
public char scanChar(){
int c=scan();
while (isWhiteSpace(c))c=scan();
return (char)c;
}
public int scanInt() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
public long scanLong() throws IOException {
long integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public void scanLong(long[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanLong();
}
public void scanInt(int[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanInt();
}
public double scanDouble() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
double res = 0;
while (!isWhiteSpace(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
res *= 10;
res += c - '0';
c = scan();
}
if (c == '.') {
c = scan();
double m = 1;
while (!isWhiteSpace(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
m /= 10;
res += (c - '0') * m;
c = scan();
}
}
return res * sgn;
}
}
优点:
扫描输入比BufferReader快
降低时间复杂度
为下一次输入刷新缓冲区
方法:
scanChar() -扫描单个字符
scanInt() - scan整型值
scanLong() -扫描长值
scanString() -扫描字符串的值
scanouble() -扫描Double值
scanInt(int[] array) -扫描完整的数组(整数)
scanLong(long[] array) -扫描完整的数组(long)
用法:
复制java代码下面的给定代码。
为给定的类初始化对象
ScanReader sc = new ScanReader(System.in);
3。导入必要的类:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
4。 从你的主方法抛出IOException来处理Exception
5。 使用提供的方法。
6。 享受
例子:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
class Main{
public static void main(String... as) throws IOException{
ScanReader sc = new ScanReader(System.in);
int a=sc.scanInt();
System.out.println(a);
}
}
class ScanReader....
# # #sc.nextLine()
is be比解析输入要好。
因为就表现而言,它是好的。
我想我已经迟到了。
如前所述,在获得你的int值后调用input。nextLine()将解决你的问题。您的代码不能工作的原因是没有其他东西可以从您的输入(在您输入int的地方)存储到string1中。我将对整个话题做一些解释。
可以将nextLine()视为Scanner类中的nextFoo()方法中的一个。让我们举个简单的例子。假设我们有如下两行代码:
int firstNumber = input.nextInt();
int secondNumber = input.nextInt();
如果我们输入下面的值(作为一行输入)
234年54
firstNumber和secondNumber变量的值分别为54和234。这样做的原因是,当nextInt()方法接受值时,不会自动生成新的换行符(即n)。它只接受“下一个int”,然后继续。除了nextLine()之外,其他的nextFoo()方法也是一样的。
nextLine()在获取一个值后立即生成一个新换行;这就是@RohitJain所说的新换行被“消耗”的意思。
最后,next()方法简单地获取最近的字符串,而不生成新行;这使得它成为在同一行中获取单独字符串的首选方法。
我希望这对你有帮助。编码快乐!
# # #
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
###如果我期望一个非空的输入
避免:
- - - - - - ?如果下列输入被未选中的scan.nextLine()
as workaround
作为解决方案< br / >
- - - - - - ?仅部分读行导致数据丢失,因为scan.nextLine()
was replaced by scan.next()
(enter: "yi被取代了scan.next()
(enter: "yi(输入:“yippie?ya?yeah”)
- - - - - - ?Exception
s thS在解析输入时抛出Scanner
methods (read first parse afterwards)
public static Function<Scanner,String> scanLine = (scan -> {
String s = scan.nextLine();
return( s.length() == 0 ? scan.nextLine() : s );
});
在上面的例子中:
System.out.println("Enter numerical value");
int option = input.nextInt(); // read numerical value from input
System.out.println("Enter 1st string");
String string1 = scanLine.apply( input ); // read 1st string
System.out.println("Enter 2nd string");
String string2 = scanLine.apply( input ); // read 2nd string
###使用两个扫描器而不是一个
Scanner input = new Scanner(System.in);
System.out.println("Enter numerical value");
int option;
Scanner input2 = new Scanner(System.in);
option = input2.nextInt();
### #在我的一个用例中,我有一个读取字符串值之前的一对整数值的场景。我必须使用“for / while循环”来读取值。在这种情况下,上述建议都不起作用。
使用input。next()而不是input。nextLine()修复了这个问题。希望这对那些遇到类似情况的人有所帮助。
# # #,nextXXX()
methods 方法不读newline
except nextLi除了
nextLine()
. We can. 我们可以跳过newline
after reading any在阅读任何non-string
value (int值(
int
in this在这种情况下)使用scanner.skip()
as below:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(x);
double y = sc.nextDouble();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(y);
char z = sc.next().charAt(0);
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(z);
String hello = sc.nextLine();
System.out.println(hello);
float tt = sc.nextFloat();
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println(tt);
###使用这个代码,它会解决你的问题。
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
input.nextLine();
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
###要解决这个问题,只需创建一个scan。 nextline(),其中scan是Scanner对象的一个实例。例如,我使用一个简单的HackerRank问题来解释。
package com.company;
import java.util.Scanner;
public class hackerrank {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // This line shall stop the skipping the nextLine()
String s = scan.nextLine();
scan.close();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
# # #nextLine()
will read将直接读取输入为空行,而不等待文本。
简单的解决方案是添加一个额外的扫描器来消耗空行:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
input.nextLine();
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
###为什么不每次阅读都使用一个新的扫描仪呢?像下面。用这种方法,你将不会面对你的问题。
int i = new Scanner(System.in).nextInt();
问题是input。nextInt()方法——它只读取int值。因此,当您继续使用input。nextLine()进行读取时,您会收到“n”回车键。因此,要跳过这一步,必须添加input。nextLine()。希望现在大家都明白了。
试试这样:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
###这是一个非常基本的问题,对于初学者在java编程。同样的问题,我也面临当我开始java(自学)。
实际上,当我们接受一个整数数据类型的输入时,它只读取整数值,并留下newLine(\n)字符和这一行(即。当我们尝试接受一个新的输入时,会产生问题。
如。比如,如果我们取整数输入,然后尝试取字符串输入。
value1=sc.nextInt();
value2=sc.nextLine();
value2将自动读取newLine字符,而不接受用户输入。
解决方案:
我们只需要在接受下一个用户输入之前添加一行代码。
sc.nextLine();
或
value1=sc.nextInt();
sc.nextLine();
value2=sc.nextLine();
Note: don't forget to close the Scanner to prevent memory leak;