日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(JAVA)Object类之Scanner

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。