(JAVA)Object类之Scanner
生活随笔
收集整理的這篇文章主要介紹了
(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,因此不展示解決辦法:?
總結
以上是生活随笔為你收集整理的(JAVA)Object类之Scanner的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vivado 2019使用教程
- 下一篇: domino生成Excel图表