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

hfwmuf9z  于 2021-07-12  发布在  Java
关注(0)|答案(21)|浏览(258)

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

qjp7pelc

qjp7pelc1#

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

drkbr07n2#

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

tyu7yeag3#

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

mepcadol

mepcadol4#

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

w8ntj3qf

w8ntj3qf5#

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

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

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

j8ag8udp

j8ag8udp6#

热释光;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 .

l7mqbcuq

l7mqbcuq7#

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

bogh5gae8#

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

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

mec1mxoz9#

如果您想快速扫描输入而不被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....
g6baxovj

g6baxovj10#

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

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

ztigrdn811#

sc.nextLine() 比解析输入更好。因为从性能上看,它是好的。

kx5bkwkv

kx5bkwkv12#

我想我去派对已经很晚了。。
如前所述,呼叫 input.nextLine() 得到int值后,问题就迎刃而解了。你的代码不起作用的原因是因为你的输入(输入int的地方)中没有其他东西可以存储 string1 . 我只想对整个主题多讲一点。
将nextline()看作scanner类中nextfoo()方法中的奇数。让我们举个简单的例子。。假设我们有两行代码,如下所示:

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

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

gijlo24d

gijlo24d13#

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

w8ntj3qf14#

如果我期望一个非空的输入
避免:
–  如果以下输入被未选中的 scan.nextLine() 作为解决方法
–  仅部分读取行导致数据丢失,因为 scan.nextLine() 被替换为 scan.next() (输入:“yippie 是的 是的“)
–  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
8zzbczxx

8zzbczxx15#

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

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

相关问题