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

vuktfyat  于 2021-07-03  发布在  Java
关注(0)|答案(21)|浏览(273)

我用的是 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() 按我的意愿执行。

czq61nw1

czq61nw116#

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

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

vaj7vani17#

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

}

gmol1639

gmol163918#

问题在于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();
jum4pzuy

jum4pzuy19#

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

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

bvk5enib20#

热释光;dr使用 scanner.skip("\\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 .

mnowg1ta

mnowg1ta21#

那是因为 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();

相关问题