Java基础 Scanner 使用nextInt接收整数

  •     JDK :OpenJDK-11
  •      OS :CentOS 7.6.1810
  •      IDE :Eclipse 2019‑03
  • typesetting :Markdown

code

package per.jizuiku.base;

import java.util.Scanner;

/**
 * @author 给最苦
 * @date 2019/06/29
 * @blog www.cnblogs.com/jizuiku
 */
class Demo {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个数据");
        int x = sc.nextInt();
        System.out.println("你所输入的数据是:" + x);

        sc.close();
    }
}

result

请输入一个数据
55
你所输入的数据是:55

sourceCode

/**
    * Constructs a new {@code Scanner} that produces values scanned
    * from the specified input stream. Bytes from the stream are converted
    * into characters using the underlying platform's
    * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
    *
    * @param  source An input stream to be scanned
    */
public Scanner(InputStream source) {
    this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
/**
    * Scans the next token of the input as an {@code int}.
    *
    * 

An invocation of this method of the form * {@code nextInt()} behaves in exactly the same way as the * invocation {@code nextInt(radix)}, where {@code radix} * is the default radix of this scanner. * * @return the {@code int} scanned from the input * @throws InputMismatchException * if the next token does not match the Integer * regular expression, or is out of range * @throws NoSuchElementException if input is exhausted * @throws IllegalStateException if this scanner is closed */ public int nextInt() { return nextInt(defaultRadix); }

/**
    * Scans the next token of the input as an {@code int}.
    * This method will throw {@code InputMismatchException}
    * if the next token cannot be translated into a valid int value as
    * described below. If the translation is successful, the scanner advances
    * past the input that matched.
    *
    * 

If the next token matches the Integer regular expression defined * above then the token is converted into an {@code int} value as if by * removing all locale specific prefixes, group separators, and locale * specific suffixes, then mapping non-ASCII digits into ASCII * digits via {@link Character#digit Character.digit}, prepending a * negative sign (-) if the locale specific negative prefixes and suffixes * were present, and passing the resulting string to * {@link Integer#parseInt(String, int) Integer.parseInt} with the * specified radix. * *

If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX} * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an * {@code IllegalArgumentException} is thrown. * * @param radix the radix used to interpret the token as an int value * @return the {@code int} scanned from the input * @throws InputMismatchException * if the next token does not match the Integer * regular expression, or is out of range * @throws NoSuchElementException if input is exhausted * @throws IllegalStateException if this scanner is closed * @throws IllegalArgumentException if the radix is out of range */ 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()); } }

resource

  • [ JDK ] openjdk.java.net
  • [ doc - 参考 ] docs.oracle.com/en/java/javase/11
  • [ 规范 - 推荐 ] yq.aliyun.com/articles/69327
  • [ 规范 - 推荐 ] google.github.io/styleguide
  • [ 源码 ] hg.openjdk.java.net
  • [ OS ] www.centos.org
  • [ IDE ] www.eclipse.org/downloads/packages
  • [ 平台 ] www.cnblogs.com


感谢帮助过 给最苦 的人们。
Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。
规范的命名和代码格式等,有助于沟通和理解。
JVM的配置、监控与优化,比较实用,值得学习。

转载于:https://www.cnblogs.com/jizuiku/p/11107751.html

你可能感兴趣的:(Java基础 Scanner 使用nextInt接收整数)