Integer.parseInt(scanner.nextLine()) 与scanner.nextInt()

Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()(Integer.parseInt(scanner.nextLine()) 与scanner.nextInt())

本文介绍了Integer.parseInt(scanner.nextLine()) 与scanner.nextInt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授倾向于执行以下操作以从用户那里获取数字:

My professor tends to do the following to get a number from the user:

Scanner scanner = new Scanner(System.in);
Integer.parseInt(scanner.nextLine());

与简单地做 scanner.nextInt() 相比有什么好处?

What are the benefits as opposed to simply doing scanner.nextInt() ?

java.util.Scanner.java 包含以下内容:

public int nextInt() {
    return nextInt(defaultRadix);
}

public int nextInt(int radix) {
    // Check cached result
    if ((typeCache != null) && (typeCache instanceof Integer)
        && this.radix == radix) {
        int val = ((Integer)typeCache).intValue();
        useTypeCache();
        return val;
    }
    setRadix(radix);
    clearCaches();
    // Search for next int
    try {
        String s = next(integerPattern());
        if (matcher.group(SIMPLE_GROUP_INDEX) == null)
            s = processIntegerToken(s);
        return Integer.parseInt(s, radix);
    } catch (NumberFormatException nfe) {
        position = matcher.start(); // don't skip bad token
        throw new InputMismatchException(nfe.getMessage());
    }
}

在我看来,Scanner 本身也调用 Integer.parseInt() ,在额外的恶作剧之上.做简单的 Integer.parseInt(scanner.nextLine()) 有显着的性能提升吗?另一方面有什么缺点吗?

As I see it, Scanner calls Integer.parseInt() itself as well, on top of additional hocus pocus. Are there significant performance gains in doing simply Integer.parseInt(scanner.nextLine()) ? Are there on the other hand any drawbacks?

扫描包含大量数据而不是用户输入的文件时会怎样?

How about when scanning through a file with significant amount of data, and not a user input?

推荐答案

有2个观察:

  1. 使用 myScannerInstance.nextInt() 会留下一个换行符.因此,如果您在 nextInt() 之后调用 nextLine(),则 nextLine() 将读取换行符而不是实际数据.因此,您必须在 nextInt() 之后添加另一个 nextLine() 来吞噬那个 dangling 换行符.nextLine() 不会留下换行符.
  1. Using myScannerInstance.nextInt() leaves behind a new line character. So, if you call nextLine() after nextInt(), the nextLine() will read the new line character instead of the actual data. Consequently, you will have to add another nextLine() after the nextInt() to gobble up that dangling new-line character. nextLine() doesn't leave behind a new line character.

代码:

int age=myScannerInstance.nextInt();
String name = myScannerInstance.nextLine();// here the actual name will not be read. The new line character will be read.

  1. nextInt() 将再次返回底层流并读取.IO 调用需要时间(昂贵).它将进行大量检查以获取下一个整数.nextLine() 只会做一次这些检查.因此,如果您调用一次 nextLine() 并读取 5 个整数(作为单行字符串),将它们拆分并解析为整数(使用 Integer.parseInt()),这将比单独读取每个 int 更快、更高效.
  1. nextInt() will again go back to the underlying stream and read. IO calls take time (expensive). It will do lot of checks to get the next integer. nextLine() will do those checks only once. So, if you call nextLine() once and read 5 integers (as a single line String), split them and parse them as integers (using Integer.parseInt()), it will be faster and more efficient than reading each int individually.

在运行非常大的循环时,使用 nextLine() + parseInt() 将为您带来巨大的性能优势.

Using nextLine() + parseInt() will give you enormous performance benefit when you are running a very large loop.

用法:

使用 nextInt() 给你一个额外的好处,如果输入文本不是整数,你会得到一个异常.示例 123 被接受.123sdsa 将抛出 InputMismatchException.所以,你可以抓住它并适当地处理它.

Using nextInt() gives you an additional advantage wherein you will get an exception if the input text is not an integer. example 123 is accepted.. 123sdsa will throw an InputMismatchException. So, you can catch it and handle it appropriately.

使用 nextLine() 将读取整行,因此,它将读取整个字符串 sada1231 然后如果它失败并返回 NumberFormatException无法将字符串解析为数字.您必须处理该异常.

Using nextLine() will read the entire line, so, it will read the entire String sada1231 and then fail with NumberFormatException if it cannot parse the String as a number. You will have to handle that exception.

通常,一个 nextLine()/nextInt() 调用不会有太大的不同.如果你有一个循环或者你正在读取大量数据,那么使用 readLine()parseInt() 会非常有效.

Generally, one nextLine() / nextInt() call won't make much of a difference. If you have a loop or if you are reading lot of data, then using readLine() with parseInt() will be very efficient.

这篇关于Integer.parseInt(scanner.nextLine()) 与scanner.nextInt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Integer.parseInt(scanner.nextLine()) 与scanner.nextInt()

基础教程推荐