• <small id='9v8MP'></small><noframes id='9v8MP'>

      1. <i id='9v8MP'><tr id='9v8MP'><dt id='9v8MP'><q id='9v8MP'><span id='9v8MP'><b id='9v8MP'><form id='9v8MP'><ins id='9v8MP'></ins><ul id='9v8MP'></ul><sub id='9v8MP'></sub></form><legend id='9v8MP'></legend><bdo id='9v8MP'><pre id='9v8MP'><center id='9v8MP'></center></pre></bdo></b><th id='9v8MP'></th></span></q></dt></tr></i><div id='9v8MP'><tfoot id='9v8MP'></tfoot><dl id='9v8MP'><fieldset id='9v8MP'></fieldset></dl></div>
        • <bdo id='9v8MP'></bdo><ul id='9v8MP'></ul>
      2. <legend id='9v8MP'><style id='9v8MP'><dir id='9v8MP'><q id='9v8MP'></q></dir></style></legend>

        <tfoot id='9v8MP'></tfoot>

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

        时间:2023-10-02

              <tbody id='Rx66I'></tbody>
          • <tfoot id='Rx66I'></tfoot><legend id='Rx66I'><style id='Rx66I'><dir id='Rx66I'><q id='Rx66I'></q></dir></style></legend>
              <bdo id='Rx66I'></bdo><ul id='Rx66I'></ul>

                <i id='Rx66I'><tr id='Rx66I'><dt id='Rx66I'><q id='Rx66I'><span id='Rx66I'><b id='Rx66I'><form id='Rx66I'><ins id='Rx66I'></ins><ul id='Rx66I'></ul><sub id='Rx66I'></sub></form><legend id='Rx66I'></legend><bdo id='Rx66I'><pre id='Rx66I'><center id='Rx66I'></center></pre></bdo></b><th id='Rx66I'></th></span></q></dt></tr></i><div id='Rx66I'><tfoot id='Rx66I'></tfoot><dl id='Rx66I'><fieldset id='Rx66I'></fieldset></dl></div>

                  <small id='Rx66I'></small><noframes id='Rx66I'>

                  本文介绍了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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:new Integer(123)、Integer.valueOf(123) 和 just 123 之间的区 下一篇:Java - 将 int 更改为 ascii

                  相关文章

                  最新文章

                    <bdo id='2yvfI'></bdo><ul id='2yvfI'></ul>
                  <i id='2yvfI'><tr id='2yvfI'><dt id='2yvfI'><q id='2yvfI'><span id='2yvfI'><b id='2yvfI'><form id='2yvfI'><ins id='2yvfI'></ins><ul id='2yvfI'></ul><sub id='2yvfI'></sub></form><legend id='2yvfI'></legend><bdo id='2yvfI'><pre id='2yvfI'><center id='2yvfI'></center></pre></bdo></b><th id='2yvfI'></th></span></q></dt></tr></i><div id='2yvfI'><tfoot id='2yvfI'></tfoot><dl id='2yvfI'><fieldset id='2yvfI'></fieldset></dl></div>

                  1. <tfoot id='2yvfI'></tfoot>
                  2. <small id='2yvfI'></small><noframes id='2yvfI'>

                    1. <legend id='2yvfI'><style id='2yvfI'><dir id='2yvfI'><q id='2yvfI'></q></dir></style></legend>