• <legend id='xIpJT'><style id='xIpJT'><dir id='xIpJT'><q id='xIpJT'></q></dir></style></legend>

      <bdo id='xIpJT'></bdo><ul id='xIpJT'></ul>
  • <tfoot id='xIpJT'></tfoot>

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

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

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

        Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()(Integer.parseInt(scanner.nextLine()) 与scanner.nextInt())
        • <i id='6gysw'><tr id='6gysw'><dt id='6gysw'><q id='6gysw'><span id='6gysw'><b id='6gysw'><form id='6gysw'><ins id='6gysw'></ins><ul id='6gysw'></ul><sub id='6gysw'></sub></form><legend id='6gysw'></legend><bdo id='6gysw'><pre id='6gysw'><center id='6gysw'></center></pre></bdo></b><th id='6gysw'></th></span></q></dt></tr></i><div id='6gysw'><tfoot id='6gysw'></tfoot><dl id='6gysw'><fieldset id='6gysw'></fieldset></dl></div>

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

                <small id='6gysw'></small><noframes id='6gysw'>

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                  <i id='Xrl0N'><tr id='Xrl0N'><dt id='Xrl0N'><q id='Xrl0N'><span id='Xrl0N'><b id='Xrl0N'><form id='Xrl0N'><ins id='Xrl0N'></ins><ul id='Xrl0N'></ul><sub id='Xrl0N'></sub></form><legend id='Xrl0N'></legend><bdo id='Xrl0N'><pre id='Xrl0N'><center id='Xrl0N'></center></pre></bdo></b><th id='Xrl0N'></th></span></q></dt></tr></i><div id='Xrl0N'><tfoot id='Xrl0N'></tfoot><dl id='Xrl0N'><fieldset id='Xrl0N'></fieldset></dl></div>
                  <legend id='Xrl0N'><style id='Xrl0N'><dir id='Xrl0N'><q id='Xrl0N'></q></dir></style></legend>

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

                  <tfoot id='Xrl0N'></tfoot>
                    <tbody id='Xrl0N'></tbody>

                        • <bdo id='Xrl0N'></bdo><ul id='Xrl0N'></ul>