(JAVA)Object类之Scanner

/*
Scanner类:使用正则表达式解析基本类型和字符串的简单文本扫描器
一.源代码:public final class Scanner implements Iterator<String>, Closeable {...}1.不允许继承2.使用时必须导入包 import java.util.Scanner; jdk1.5以上版本才有3.Scanner类构造器,使用的是重载  public Scanner(InputStream source) { this(new InputStreamReader(source), WHITESPACE_PATTERN); }
二. 使用的成员方法1.接收录入的整形数据:public int nextInt() { return nextInt(defaultRadix); }/**源码:* Scans the next token of the input as an <tt>int</tt>.* This method will throw <code>InputMismatchException</code>* 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 <tt>int</tt> 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.** @param radix the radix used to interpret the token as an int value* @return the <tt>int</tt> 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 closed2.接收录入的字符串:  注意nextLine()与next()的区别public String nextLine() {if (hasNextPattern == linePattern())return getCachedResult();clearCaches();String result = findWithinHorizon(linePattern, 0);if (result == null)throw new NoSuchElementException("No line found");MatchResult mr = this.match();String lineSep = mr.group(1);if (lineSep != null)result = result.substring(0, result.length() - lineSep.length());if (result == null)throw new NoSuchElementException();elsereturn result;3.其他类型:nextBoolean()nextByte()nextDouble()nextFloat()4.boolean hasNextXXX():返回的是boolean类型,用来判断输入的类型,避免发生异常5.注意细节:先调用字符串类后调用整数类-------正常运行先调用整数类后调用字符串类-------无法录入字符串原因:整数类型后默认换行符 :  /n/t例如:输入123之后换行---------实际输入的是:123/n/tString接收的是/n/t,所以不会运行到录入字符串解决办法:1.重新创建Scanner对象2.使用对象包装类----Integer类parseInt()---将字符串转为整数类型【后续更新】

                

 
package com.Scanner.Dome;import java.util.Scanner;
public class ScannerDome {public static void main(String[] args){Scanner sc = new Scanner(System.in);int x = sc.nextInt();System.out.println(x);Scanner m = new Scanner(System.in);String a = m.nextLine();System.out.println(a);Scanner p = new Scanner(System.in);String b = p.next(); //b = "abc ef"System.out.println(b); // b = "abc",next()以空格为切割点,空格后不输出}}

nextLine()与next()的运行结果:

4.接收录入对象不对时抛出异常

判断接收对象是否符合预期:

 5.先调用字符串方法,后调用整数方法

先调用整数方法,后调用字符串

输入123之后换行---------实际输入的是:123/n/t,String接收的是:/n/t,因此不展示

解决办法: 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421845.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

序列化 小复习

想要序列化一个对象则需要使其继承serializable或者externalizable接口 一下是一个实例小程序&#xff1a; ser_test 1 import java.io.*;2 3 public class ser_test{4 public static void main(String[] args) throws Exception{5 person p1 new person(1,1.2,&q…

(JAVA)Object类之String类

/* 字符串&#xff1a; 一、概述&#xff1a;1.字符串在JAVA中&#xff0c;使用""表示2.java.lang.String类3.只要写""就是字符串对象。不需要new二、空参构造器new Sting();private final char value[];public String() {this.value "".value;…

(JAVA)String类之比较方法

/* 字符串&#xff1a; 一、概述&#xff1a;1.字符串在JAVA中&#xff0c;使用""表示2.java.lang.String类3.只要写""就是字符串对象。不需要new二、空参构造器new Sting();private final char value[];public String() {this.value "".value;…

云计算之路-黎明前的黑暗:20130424网站故障经过

一、背景 4月18日的访问高峰扛过去之后&#xff0c;我们和阿里云一直在努力寻找问题的真正原因。是问题&#xff0c;躲不去的&#xff0c;不找到根源&#xff0c;随时会突然袭击。 压力测试未能重现问题&#xff0c;只能进行大海捞针般的猜测&#xff1a;SLB&#xff08;均衡均…

hdu2955 Robberies (动态规划之背包)

http://acm.hdu.edu.cn/showproblem.php?pid2955 题意&#xff1a;Roy想要抢劫银行&#xff0c;每家银行多有一定的金额和被抓到的概率&#xff0c;知道Roy被抓的最大概率P&#xff0c;求Roy在被抓的情况下&#xff0c;抢劫最多。 分析&#xff1a;被抓概率可以转换成安全概率…

黑马Java学习笔记之-----集合框架

---------------------- android培训、java培训、期待与您交流&#xff01; ---------------------- 一&#xff0e;概述&#xff1a; Java的集合类是一种特别有用的工具类&#xff0c;它可以用于存储数量不等的多个对象&#xff08;实际上是对象的引用&#xff09;&#xff0c…

(JAVA)基本数据类型 对象包装类

package com.book.lite;/*** author zhangyu* date 2021年08月15日 4:51 下午* 基本数据类型 对象包装类* 对八个基本数据类型&#xff0c;提供8个类&#xff0c;&#xff0c;将基本数据类型&#xff0c;封装成8个对象* byte Byte* short Short* int I…

作业自动提示功能设计思路

1、利用现在FLEX项目中的心跳包机制&#xff0c;使用SOCKET心跳包技术获取最新的作业情况。 2、在现在FLEX项目中有一个&#xff1a; 核心代码&#xff1a; 这样我们可以利用这个通道&#xff0c;获取相应的信息。 具体修改步骤如下&#xff1a; 1、准备工作 创建一张表&#x…

libev源码分析--常用的watcher

在上一篇文章里&#xff0c;我们分析了libev整体设计思想和主循环的工作原理&#xff0c;也提到了watcher是衔接开发者代码的主要入口。watcher与开发者最接近&#xff0c;也与具体事件处理逻辑最接近。所以&#xff0c;watcher的具体实现&#xff0c;与性能的关系也相当密切。…

(Java)Character类

package com.book.lite;import sun.lwawt.macosx.CSystemTray;import java.util.Scanner;/*** author zhangyu* date 2021年08月16日 10:50 下午* Character类的方法* 1.判断是否小写&#xff1a;isLowerCase()* 2.判断是否大写&#xff1a;isUpperCase()* 3.判断是不是数字&am…

棋盘切割 DP POJ 1191

把方差公式先变形为 σ2 (1/n)∑xi2-xa2 xa为平均值。 由于要求标准差最小&#xff0c;只需方差最小&#xff0c;平均值都是一样的&#xff0c;n也是一样的&#xff0c;这样原问题就变为求这n快小棋盘总分的平方和最小 考虑左上角为&#xff08;x1,y1&#xff09;,右上角为&am…

lucene,lucene.net学习教程

lucene学习教程 1.1 什么是lucene Lucene是一个全文搜索框架&#xff0c;而不是应用产品。因此它并不像www.baidu.com 或者google Desktop那么拿来就能用&#xff0c;它只是提供了一种工具让你能实现这些产品。 2 lucene的工作方式 lucene提供的服务实际包含两部分&#xf…