扫描仪在使用next()或nextfoo()后跳过nextline()?

rpppsulh  于 2021-07-14  发布在  Java
关注(0)|答案(21)|浏览(324)

我用的是 Scanner 方法 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)

问题是在输入数值后,第一个 input.nextLine() 跳过第二个 input.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() 按我的意愿执行。

ttygqcqt

ttygqcqt1#

要解决此问题,只需执行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);
}

}

ve7v8dk2

ve7v8dk22#

问题在于input.nextint()方法-它只读取int值。因此,当您继续使用input.nextline()读取时,会收到“\n”enter键。因此,要跳过此操作,必须添加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();
afdcj2ne

afdcj2ne3#

为什么每次阅读都不使用新的扫描仪呢?就像下面一样。用这种方法你将不会面对你的问题。

int i = new Scanner(System.in).nextInt();
yvt65v4c

yvt65v4c4#

使用此代码可以解决您的问题。

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)
bjp0bcyl

bjp0bcyl5#

作为 nextXXX() 方法不读 newline ,除了 nextLine() . 我们可以跳过 newline 在阅读任何 non-string 价值( int 在这种情况下)使用 scanner.skip() 具体如下:

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);
omhiaaxx

omhiaaxx6#

使用两个扫描仪对象而不是一个

Scanner input = new Scanner(System.in);
System.out.println("Enter numerical value");    
int option;
Scanner input2 = new Scanner(System.in);
option = input2.nextInt();
wtlkbnrh

wtlkbnrh7#

在我的一个用例中,我有这样一个场景:读取一个字符串值,后跟几个整数值。我必须使用“for/while循环”来读取值。而上述建议在这种情况下都不起作用。
使用 input.next() 而不是 input.nextLine() 解决了这个问题。希望这对处理类似情况的人有所帮助。

hfyxw5xn

hfyxw5xn8#

那是因为 Scanner.nextInt 方法不会读取通过按“enter”创建的输入中的换行符,因此 Scanner.nextLine 在读了新行之后返回。
在使用时,您会遇到类似的行为 Scanner.nextLine 之后 Scanner.next() 或任何 Scanner.nextFoo 方法(除 nextLine 本身)。
解决方法:
要么放一个 Scanner.nextLine 每次之后打电话 Scanner.nextInt 或者 Scanner.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();
ffx8fchx

ffx8fchx9#

问题在于input.nextint()方法-它只读取int值。因此,当您继续使用input.nextline()读取时,会收到“\n”enter键。因此,要跳过此操作,必须添加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();
icnyk63a

icnyk63a10#

因为当你输入一个数字然后按回车键, input.nextInt() 只消耗数字,不消耗“行尾”。什么时候 input.nextLine() 执行时,它将使用第一个输入中仍在缓冲区中的“行尾”。
相反,使用 input.nextLine() 紧接着 input.nextInt()

lyfkaqu1

lyfkaqu111#

关于这个问题,我们似乎有很多疑问 java.util.Scanner . 我认为一个更具可读性/习惯性的解决方案是 scanner.skip("[\r\n]+") 调用后删除任何换行符 nextInt() .
编辑:正如@patrickparker在下面提到的,如果用户在数字后面输入任何空格,这将导致一个无限循环。查看他们的答案,以获得更好的模式来使用skip:https://stackoverflow.com/a/42471816/143585

ua4mk5z4

ua4mk5z412#

它这样做是因为 input.nextInt(); 没有抓住新线。你可以通过添加一个 input.nextLine(); 在下面。
或者,您也可以使用c#样式将下一行解析为整数,如下所示:

int number = Integer.parseInt(input.nextLine());

这样做同样有效,而且可以节省一行代码。

4jb9z9bj

4jb9z9bj13#

热释光;dr使用 scanner.skip("\\R") (自 skip 使用regex where \R 表示行分隔符) scanner.newLine() 调用,在以下时间后执行:
scanner.next() scanner.next*TYPE*() 方法,比如 scanner.nextInt() .

你需要知道的事情:

表示几行的文本在行之间也包含不可打印的字符(我们称之为行分隔符),如
回车符(cr—表示为 "\r" )
换行符(lf-in)表示为 "\n" )
当您从控制台读取数据时,它允许用户键入他的响应,当他完成时,他需要以某种方式确认这一事实。为此,用户需要按键盘上的“回车”/“回车”键。
重要的是,除了确保将用户数据放置到标准输入(由 System.in 它是由 Scanner )还发送依赖于操作系统的行分隔符(如windows) \r\n )在它之后。
所以当你要求用户提供 age ,用户键入42并按enter键,标准输入将包含 "42\r\n" .

问题 Scanner#nextInt (及其他) Scanner#nextType 方法)不允许扫描仪使用这些行分隔符。它会从 System.in (否则扫描器怎么知道用户没有更多代表 age 这将从标准输入中删除它们,但它也会在内部缓存这些行分隔符。我们需要记住的是,所有的scanner方法总是从缓存的文本开始扫描。

现在 Scanner#nextLine() 只需收集并返回所有字符,直到找到行分隔符(或流的结尾)。但是,由于从控制台读取数字后的行分隔符会立即在scanner的缓存中找到,因此它返回空字符串,这意味着scanner无法在这些行分隔符(或流结尾)之前找到任何字符。
顺便说一句 nextLine 也会消耗这些行分隔符。

解决方案

所以,当您要请求数字,然后请求整行,同时避免由于 nextLine ,或者
使用左边的行分隔符 nextInt 从扫描仪缓存
打电话 nextLine ,
或者我更容易理解的方法是 skip("\\R") 或者 skip("\r\n|\r|\n") 允许扫描仪跳过行分隔符匹配的部分(有关 \R : https://stackoverflow.com/a/31060125)
不要使用 nextInt (或 next ,或任何 nextTYPE 方法)。而是使用 nextLine 并将每行中的数字(假设一行只包含一个数字)解析为适当的类型,如 int 通过 Integer.parseInt .
顺便说一句: Scanner#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 .

rqmkfv5c

rqmkfv5c14#

而不是 input.nextLine() 使用 input.next() ,这应该能解决问题。
修改代码:

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();
}
uurv41yg

uurv41yg15#

如果要同时读取字符串和整数,解决方案是使用两个扫描仪:

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();

相关问题