(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…

windows设备坐标和逻辑坐标的转换

设备坐标通俗的讲就是我们看到的我们的视图坐标&#xff0c;它包含屏幕坐标、窗体坐标和客户区坐标&#xff0c;他们都是以窗体的左上角为准的坐标&#xff0c;并且是从左到右&#xff0c;从上到下的坐标 简单总结一下他的特点&#xff1a;x坐标是从左到右&#xff0c;y坐标是从…

(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;…

HADOOP__PIG安装与配置

PIG安装与配置 前提 已成功安装好Hadoop集群。 详见&#xff1a;http://www.cnblogs.com/freeideas/archive/2013/04/16/3023287.html 以下只在一个结点做&#xff0c;PIG可以直接调用整个集群。此处我选择了独立于其他节点的Server(CentOS)节点。 安装要求&#xff1a; Server…

(JAVA)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类之比较方法(2)

String类&#xff1a; 获取字符串的内容 一、获取字符串长度public int length() {return value.length;}length 与 lenght()的区别&#xff1a;前面是属性&#xff0c;不需要传参数后面是方法 二、指定下角标&#xff0c;返回固定位置的对应字符串public char charAt(int ind…

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

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

Sting练习

package com.book.lite;import java.util.Locale; import java.util.Scanner;/*1. 字符串的两种定义方式的区别:String a1 ""; 只有一个对象:String a2 new String(); 有两个对象2. String类的特点是什么底层代码的不变性&#xff0c;将字符串拆分成字符数组char[]…

枚举生成MVC3: Bind Enum To DropDownList ?-java教程

发一下牢骚和主题无关&#xff1a; 0. 预备测试数据 0.1 : 枚举 using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace MvcApplication.Models {public enum YesOrNo{否 0,是 1} } 0.2 : 对应实体类及表sql using System; using…

(JAVA)StringBuffer类

可变数组&#xff1a;StringBuffer类(静态缓冲区) 提高字符串的操作效率 StringBuffer 底层实现原理是字符数组&#xff0c;没有final StringBuffer,含有数组char[] value 默认长度是16 在JAVA中&#xff0c;数组是固定长度&#xff0c;一旦创建不能改变 StringBuffer 通过数组…

40个超酷的jQuery动画教程

原文地址&#xff1a;http://www.goodfav.com/40-cool-jquery-animation-tutorials-1189.html 由于它的到来&#xff0c;已经引起了jQuery的网络风暴&#xff0c;现在是创建漂亮的网页上的动画的首选解决方案之一。 jQuery提供了很大的跨浏览器支持&#xff0c;它是轻量级且易于…

好东西,资料!!

架构&#xff1a; http://www.cnblogs.com/lovecindywang/archive/2012/12/23/2829828.html http://www.cnblogs.com/angben/archive/2012/12/23/2829795.html http://blog.csdn.net/longxibendi/article/details/6628480 企业应用集成可以在不同层面上进行&#xff1a;例如在数…

(JAVA)StringBuffer类(练习)

package com.book.lite;import java.util.Scanner;/* 1.将数组拼接成字符串 2.自定义方法&#xff0c;将缓冲区翻转*/ public class StringBufferDemo2 {public static void main(String[] args){System.out.println(getString_1());System.out.println(getString_2());}public…

hdu2955 Robberies (动态规划之背包)

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

数组排序之冒泡排序

package com.book.lite;/*** author zhangyu* date 2021年08月11日 10:51 下午* 实现整数数组的排序&#xff0c;顺序排序&#xff0c;冒泡排序*/ public class arrDome {public static void main(String[] a){int[] arr {2,0,9,8,67,-45,-199}; // smallNumber(arr); …