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

s5a0g9ez  于 2021-08-25  发布在  Java
关注(0)|答案(20)|浏览(281)

我正在使用 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() 按照我的要求执行。

p1tboqfb

p1tboqfb1#

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

lc8prwob2#

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

eni9jsuy3#

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

jecbmhm3

jecbmhm34#

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

lc8prwob

lc8prwob5#

它这样做是因为 input.nextInt(); 没有捕捉到新线。您可以通过添加 input.nextLine(); 在下面
或者,您可以采用c#风格,将下一行解析为整数,如下所示:

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

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

ivqmmu1c

ivqmmu1c6#

tl;博士
使用 scanner.skip("\\R") (自 skip 在何处使用正则表达式 \R 表示每行之前的行分隔符) scanner.newLine() 调用,该调用在以下时间之后执行:
scanner.next() scanner.next*TYPE*() 方法,比如 scanner.nextInt() .
或更安全的变体: scanner.skip("\\R?") 在每个之前 scanner.nextLine() 如果你不确定之后是否会调用它 scanner.next()scanner.next*TypeName*() . ? 将使行分隔符序列可选(这将防止 skip (a)中的方法等待匹配序列-如果数据源仍然打开,如 System.in (b) 投掷 java.util.NoSuchElementException 如果是终止/结束的数据源(如文件或字符串)

你需要知道的事情:

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

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

现在 Scanner#nextLine() 只需收集并返回所有字符,直到找到行分隔符(或流结束)。但由于从控制台读取数字后,行分隔符会立即在扫描仪的缓存中找到,因此它返回空字符串,这意味着扫描仪无法在这些行分隔符(或流结束)之前找到任何字符。
顺便提一下 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 .

1aaf6o9v

1aaf6o9v7#

而不是 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();
}
owfi6suc

owfi6suc8#

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

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

7ajki6be9#

如果您想快速扫描输入而不被scanner类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()-扫描整数值
scanlong()-扫描长值
scanstring()-扫描字符串值
scandouble()-扫描双值
scanint(int[]数组)-扫描整个数组(整数)
scanlong(long[]数组)-扫描整个数组(long)

用法:

将给定代码复制到java代码下面。
初始化给定类的对象 ScanReader sc = new ScanReader(System.in); 3.导入必要的类: import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; 4.从主方法抛出ioexception以处理异常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....
utugiqy6

utugiqy610#

为了避免该问题,请使用 nextLine(); 紧接着 nextInt(); 因为它有助于清除缓冲区。当你按下 ENTER 这个 nextInt(); 不捕获新行,因此跳过 Scanner 稍后再编码。

Scanner scanner =  new Scanner(System.in);
int option = scanner.nextInt();
scanner.nextLine(); //clearing the buffer
iih3973s

iih3973s11#

sc.nextLine() 与解析输入相比,它的性能更好。因为就性能而言,这将是好的。

deikduxw

deikduxw12#

我想我参加晚会已经很晚了。。
如前所述 input.nextLine() 得到int值后,将解决您的问题。您的代码不起作用的原因是,您的输入(输入int的地方)中没有其他内容可存储 string1 . 我只想对整个主题多说一点。
将NestRoin()视为扫描器类中的NeXFoT()方法中的奇数。让我们举一个很快的例子。假设我们有两行代码,如下所示:

int firstNumber = input.nextInt();
int secondNumber = input.nextInt();

如果我们输入以下值(作为一行输入)
54 234
我们的价值 firstNumbersecondNumber 变量分别变为54和234。这种方式工作的原因是,当nextint()方法接受值时,不会自动生成新行馈送(即\n)。它只需要“next int”并继续。除了nextline()之外,其余的nextfoo()方法也是如此。
nextline()在获取值后立即生成一个新行提要;这就是@rohitjain所说的新行提要被“消耗”的意思。
最后,next()方法只获取最近的字符串,而不生成新行;这使得它成为在同一行中获取单独字符串的首选方法。
我希望这有帮助。。快乐的编码!

ar7v8xwq

ar7v8xwq13#

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);
    }
rm5edbpk

rm5edbpk14#

如果我期望一个非空的输入
避免:
–如果以下输入被未经检查的 scan.nextLine() 作为解决办法
–仅部分读取行导致数据丢失,因为 scan.nextLine() 被替换为 scan.next() (输入:“Yeppie ya yeah”)
–  Exception 使用解析输入时抛出的 Scanner 方法(先读取,后解析)

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
9avjhtql

9avjhtql15#

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

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

相关问题