Java Scanner類用法及nextLine()產(chǎn)生的換行符問題實例分析
本文實例講述了Java Scanner類用法及nextLine()產(chǎn)生的換行符問題。分享給大家供大家參考,具體如下:
分析理解:Scanner sc = new Scanner(System.in);
package cn.itcast_01;
/*
* Scanner:用于接收鍵盤錄入數(shù)據(jù)。
*
* 前面的時候:
* A:導(dǎo)包
* B:創(chuàng)建對象
* C:調(diào)用方法
*
* 分析理解:Scanner sc = new Scanner(System.in);
* System類下有一個靜態(tài)的字段:
* public static final InputStream in; 標準的輸入流,對應(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)建對象
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)建對象
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;
/*
* 常用的兩個方法:
* public int nextInt():獲取一個int類型的值
* public String nextLine():獲取一個String類型的值
*
* 出現(xiàn)問題了:
* 先獲取一個數(shù)值,在獲取一個字符串,會出現(xiàn)問題。
* 主要原因:就是那個換行符號的問題。
* 如何解決呢?
* A:先獲取一個數(shù)值后,在創(chuàng)建一個新的鍵盤錄入對象獲取字符串。
* B:把所有的數(shù)據(jù)都先按照字符串獲取,然后要什么,你就對應(yīng)的轉(zhuǎn)換為什么。
*/
public class ScannerDemo {
public static void main(String[] args) {
// 創(chuàng)建對象
Scanner sc = new Scanner(System.in);
// 獲取兩個int類型的值
// int a = sc.nextInt();
// int b = sc.nextInt();
// System.out.println("a:" + a + ",b:" + b);
// System.out.println("-------------------");
// 獲取兩個String類型的值
// String s1 = sc.nextLine();
// String s2 = sc.nextLine();
// System.out.println("s1:" + s1 + ",s2:" + s2);
// System.out.println("-------------------");
// 先獲取一個字符串,在獲取一個int值
// String s1 = sc.nextLine();
// int b = sc.nextInt();
// System.out.println("s1:" + s1 + ",b:" + b);
// System.out.println("-------------------");
// 先獲取一個int值,在獲取一個字符串,這里會出問題
// 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é)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java帶復(fù)選框的樹(Java CheckBox Tree)實現(xiàn)和應(yīng)用
這篇文章主要為大家詳細介紹了Java帶復(fù)選框的樹實現(xiàn)和應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
使用Mybatis Generator結(jié)合Ant腳本快速自動生成Model、Mapper等文件的方法
這篇文章主要介紹了使用Mybatis Generator結(jié)合Ant腳本快速自動生成Model、Mapper等文件的方法的相關(guān)資料,需要的朋友可以參考下2016-06-06
關(guān)于springboot加載yml配置文件的no字段自動轉(zhuǎn)義問題
這篇文章主要介紹了關(guān)于springboot加載yml配置文件的no字段自動轉(zhuǎn)義問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
解決SpringBoot中使用@Transactional注解遇到的問題
這篇文章主要介紹了SpringBoot中使用@Transactional注解遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
JAVA 16位ID生成工具類含16位不重復(fù)的隨機數(shù)數(shù)字+大小寫
這篇文章主要介紹了JAVA 16位ID生成工具類含16位不重復(fù)的隨機數(shù)數(shù)字+大小寫,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

