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}.
*
* <p> 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 <i>Integer</i>
* 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.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> 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.
*
* <p>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 <i>Integer</i>
* 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的配置、监控与优化,比较实用,值得学习。
Java基础 Scanner 使用nextInt接收整数的更多相关文章
- Java基础 Scanner 使用nextLine接收字符串
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- 【Java】Scanner类nextInt后使用nextLine无法读取输入
首先,我们先介绍一下,在学习Java语言,对于字符串的输入,由于Scanner.next()函数无法输入空格及回车,此时,我们就必须要用Scanner.nextLine()解决这类问题, 在使用过程中 ...
- java基础——Scanner的基础和进阶
Scanner对象 目的:用来获取用户的输入 基本语法: Scanner s = new scanner (System.in); 通过Scanner 类的next()和nextLine()方法,获取 ...
- 解决一个无聊的问题,如何处理Java用户在dos被收集信息时拷贝带换行符的文本信息造成的while的多次循环(java解决Scanner.next在接收用户输入时出现多个换行的形况)[解决方案一]
问题描述: 用户在dos窗口输入的时候(web项目不会出现这样的问题,所以这个问题日常碰不到),摁下回车时,Scanner对象的next()扫描用户输入的文本,后面就可以根据输入的字符串进行判断,并执 ...
- java中Scanner类nextInt之后用nextLine无法读取输入
http://blog.csdn.net/wjy1090233191/article/details/42080029 这篇文章写得非常详细和准确
- 当Editplus遇到Java的Scanner
学习Java编程时,我想让变量的值从键盘输入接收进来.平时在dos中运行效果很直观,那么我在Editplus这款开发工具中也可以输入,Editplus是带有控制台.当你运行Java程序时,此时出现的编 ...
- Java基础复习3
循环的嵌套 public class demo8 { public static void main(String[] args) { /* 输出######## ...
- java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量
package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...
- Java基础(Scanner、Random、流程控制语句)
第3天 Java基础语法 今日内容介绍 u 引用数据数据类型(Scanner.Random) u 流程控制语句(if.for.while.dowhile.break.continue) 第1章 引用数 ...
随机推荐
- CentOS7安装Redis单实例
由于环境差异,安装过程可能遇到各种各样的问题,不要慌,根据错误提示解决即可. 1.下载redis下载地址在:redis.io比如把Redis安装到/usr/local/soft/ cd /usr/lo ...
- 2013.5.23 - KDD第三十五天
看完睡不觉得世间有点虚度,然后就构思了一下带带回儿去找中秋要跟她说的事情,大概就是这样的: 我 打算用paper来计算人与人之间的距离,比如说我跟郑茂和写过一篇文章,然后郑茂根韩冰和写过一篇文章, ...
- Java内存模型、JVM内存结构和Java对象模型
JVM内存结构 我们都知道,Java代码是要运行在虚拟机上的,而虚拟机在执行Java程序的过程中会把所管理的内存划分为若干个不同的数据区域,这些区域都有各自的用途.其中有些区域随着虚拟机进程的启动而存 ...
- Analysis of algorithms: introduction
一系列的人物角色 Programmer,client,theoretician和blocking 学生可能会承担里面的一个或者多个角色 Running time 提出running time这个概念的 ...
- java之比较器
java中的比较器有两种: 1.实体类实现Comparable接口,并实现其中的compareTo方法 2.在外部定义实现Comparator接口的比较器类,并实现其中的compare方法 Compa ...
- href = '' 表示刷新当前页面
<a href="javascript:;" target="_blank"><img src="../img/focus-slid ...
- SQL 删除字段 增加字段
SQL增加字段需要用到sql语句 ALTER TABLE 加(表格名称) ADD 加(字段名称) 加(字段类型)实例:ALTER TABLE T_Basic ADD SEODescription Nv ...
- HiveQL Index 索引
Hive只有有限的索引功能.Hive中没有普通关系型数据库中键的概念,但是还是可以对一些字段建立索引来加速某些操作.一张表的索引数据存储在另外一张表中. 通过explain命令可以查看某个查询语句是否 ...
- Ceph osd故障恢复
1 调高osd的日志等级 加上红框那一行就可以了 osd的日志路径:/var/log/ceph/ceph-osd.3.log 注意:加上了这一行后日志会刷很多,所以要特别注意日志容量的变化,以防把v ...
- 安装vs code之后,win+e快捷键打开的是vs code,而不是文件管理器,解决方法
安装vs code之后,win+e快捷键打开的是vs code,而不是文件管理器,解决方法 xdg-mime default dde-file-manager.desktop inode/direct ...