Java Scanner類用法及nextLine()產(chǎn)生的換行符問題實(shí)例分析
本文實(shí)例講述了Java Scanner類用法及nextLine()產(chǎn)生的換行符問題。分享給大家供大家參考,具體如下:
分析理解:Scanner sc = new Scanner(System.in);
package cn.itcast_01; /* * Scanner:用于接收鍵盤錄入數(shù)據(jù)。 * * 前面的時(shí)候: * A:導(dǎo)包 * B:創(chuàng)建對(duì)象 * C:調(diào)用方法 * * 分析理解:Scanner sc = new Scanner(System.in); * System類下有一個(gè)靜態(tài)的字段: * public static final InputStream in; 標(biāo)準(zhǔn)的輸入流,對(duì)應(yīng)著鍵盤錄入。 * * InputStream is = System.in; * * class Demo { * public static final int x = 10; * public static final Student s = new Student(); * } * int y = Demo.x; * Student s = Demo.s; * * * 構(gòu)造方法: * Scanner(InputStream source) */ import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { // 創(chuàng)建對(duì)象 Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println("x:" + x); } }
Scanner類的hasNextInt()
和nextInt()
方法
package cn.itcast_02; import java.util.Scanner; /* * 基本格式: * public boolean hasNextXxx():判斷是否是某種類型的元素 * public Xxx nextXxx():獲取該元素 * * 舉例:用int類型的方法舉例 * public boolean hasNextInt() * public int nextInt() * * 注意: * InputMismatchException:輸入的和你想要的不匹配 */ public class ScannerDemo { public static void main(String[] args) { // 創(chuàng)建對(duì)象 Scanner sc = new Scanner(System.in); // 獲取數(shù)據(jù) if (sc.hasNextInt()) { int x = sc.nextInt(); System.out.println("x:" + x); } else { System.out.println("你輸入的數(shù)據(jù)有誤"); } } }
Scanner類中的nextLine()
產(chǎn)生的換行符問題
package cn.itcast_03; import java.util.Scanner; /* * 常用的兩個(gè)方法: * public int nextInt():獲取一個(gè)int類型的值 * public String nextLine():獲取一個(gè)String類型的值 * * 出現(xiàn)問題了: * 先獲取一個(gè)數(shù)值,在獲取一個(gè)字符串,會(huì)出現(xiàn)問題。 * 主要原因:就是那個(gè)換行符號(hào)的問題。 * 如何解決呢? * A:先獲取一個(gè)數(shù)值后,在創(chuàng)建一個(gè)新的鍵盤錄入對(duì)象獲取字符串。 * B:把所有的數(shù)據(jù)都先按照字符串獲取,然后要什么,你就對(duì)應(yīng)的轉(zhuǎn)換為什么。 */ public class ScannerDemo { public static void main(String[] args) { // 創(chuàng)建對(duì)象 Scanner sc = new Scanner(System.in); // 獲取兩個(gè)int類型的值 // int a = sc.nextInt(); // int b = sc.nextInt(); // System.out.println("a:" + a + ",b:" + b); // System.out.println("-------------------"); // 獲取兩個(gè)String類型的值 // String s1 = sc.nextLine(); // String s2 = sc.nextLine(); // System.out.println("s1:" + s1 + ",s2:" + s2); // System.out.println("-------------------"); // 先獲取一個(gè)字符串,在獲取一個(gè)int值 // String s1 = sc.nextLine(); // int b = sc.nextInt(); // System.out.println("s1:" + s1 + ",b:" + b); // System.out.println("-------------------"); // 先獲取一個(gè)int值,在獲取一個(gè)字符串,這里會(huì)出問題 // int a = sc.nextInt(); // String s2 = sc.nextLine(); // System.out.println("a:" + a + ",s2:" + s2); // System.out.println("-------------------"); int a = sc.nextInt(); Scanner sc2 = new Scanner(System.in); String s = sc2.nextLine(); System.out.println("a:" + a + ",s:" + s); } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java帶復(fù)選框的樹(Java CheckBox Tree)實(shí)現(xiàn)和應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Java帶復(fù)選框的樹實(shí)現(xiàn)和應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11使用Mybatis Generator結(jié)合Ant腳本快速自動(dòng)生成Model、Mapper等文件的方法
這篇文章主要介紹了使用Mybatis Generator結(jié)合Ant腳本快速自動(dòng)生成Model、Mapper等文件的方法的相關(guān)資料,需要的朋友可以參考下2016-06-06關(guān)于springboot加載yml配置文件的no字段自動(dòng)轉(zhuǎn)義問題
這篇文章主要介紹了關(guān)于springboot加載yml配置文件的no字段自動(dòng)轉(zhuǎn)義問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java獲取漢字對(duì)應(yīng)的拼音(全拼或首字母)
這篇文章主要介紹了Java如何獲取漢字對(duì)應(yīng)的拼音(全拼或首字母),文中實(shí)現(xiàn)的方法是引用了pinyin4j-2.5.0.jar,然后給出了完整的示例代碼,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01解決SpringBoot中使用@Transactional注解遇到的問題
這篇文章主要介紹了SpringBoot中使用@Transactional注解遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09JAVA 16位ID生成工具類含16位不重復(fù)的隨機(jī)數(shù)數(shù)字+大小寫
這篇文章主要介紹了JAVA 16位ID生成工具類含16位不重復(fù)的隨機(jī)數(shù)數(shù)字+大小寫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02