Java Scanner類(lèi)用法及nextLine()產(chǎn)生的換行符問(wèn)題實(shí)例分析
本文實(shí)例講述了Java Scanner類(lèi)用法及nextLine()產(chǎn)生的換行符問(wèn)題。分享給大家供大家參考,具體如下:
分析理解:Scanner sc = new Scanner(System.in);
package cn.itcast_01;
/*
* Scanner:用于接收鍵盤(pán)錄入數(shù)據(jù)。
*
* 前面的時(shí)候:
* A:導(dǎo)包
* B:創(chuàng)建對(duì)象
* C:調(diào)用方法
*
* 分析理解:Scanner sc = new Scanner(System.in);
* System類(lèi)下有一個(gè)靜態(tài)的字段:
* public static final InputStream in; 標(biāo)準(zhǔn)的輸入流,對(duì)應(yīng)著鍵盤(pán)錄入。
*
* 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類(lèi)的hasNextInt()和nextInt()方法
package cn.itcast_02;
import java.util.Scanner;
/*
* 基本格式:
* public boolean hasNextXxx():判斷是否是某種類(lèi)型的元素
* public Xxx nextXxx():獲取該元素
*
* 舉例:用int類(lèi)型的方法舉例
* 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類(lèi)中的nextLine()產(chǎn)生的換行符問(wèn)題
package cn.itcast_03;
import java.util.Scanner;
/*
* 常用的兩個(gè)方法:
* public int nextInt():獲取一個(gè)int類(lèi)型的值
* public String nextLine():獲取一個(gè)String類(lèi)型的值
*
* 出現(xiàn)問(wèn)題了:
* 先獲取一個(gè)數(shù)值,在獲取一個(gè)字符串,會(huì)出現(xiàn)問(wèn)題。
* 主要原因:就是那個(gè)換行符號(hào)的問(wèn)題。
* 如何解決呢?
* A:先獲取一個(gè)數(shù)值后,在創(chuàng)建一個(gè)新的鍵盤(pán)錄入對(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類(lèi)型的值
// int a = sc.nextInt();
// int b = sc.nextInt();
// System.out.println("a:" + a + ",b:" + b);
// System.out.println("-------------------");
// 獲取兩個(gè)String類(lèi)型的值
// 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ì)出問(wèn)題
// 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)容感興趣的讀者可查看本站專(zhuān)題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java多線(xiàn)程處理千萬(wàn)級(jí)數(shù)據(jù)更新操作
這篇文章主要為大家詳細(xì)介紹了Java如何通過(guò)多線(xiàn)程處理千萬(wàn)級(jí)數(shù)據(jù)更新操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
關(guān)于request.getRequestDispatcher().forward()的妙用及DispatcherType
這篇文章主要介紹了關(guān)于request.getRequestDispatcher().forward()的妙用及DispatcherType對(duì)Filter配置的影響,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
IDEA打開(kāi)java項(xiàng)目后里面的java文件不能運(yùn)行解決辦法
這篇文章主要給大家介紹了關(guān)于IDEA打開(kāi)java項(xiàng)目后里面的java文件不能運(yùn)行的解決辦法,有時(shí)候想運(yùn)行別人的項(xiàng)目,但是別人的項(xiàng)目并非IDEA項(xiàng)目(甚至只有源碼),當(dāng)我們打開(kāi)項(xiàng)目時(shí)候,并不能運(yùn)行,需要的朋友可以參考下2023-10-10
Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解
這篇文章主要介紹了Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解,如果實(shí)現(xiàn)了ImportSelector接口,在配置類(lèi)中被@Import加入到Spring容器中以后,Spring容器就會(huì)把ImportSelector接口方法返回的字符串?dāng)?shù)組中的類(lèi)new出來(lái)對(duì)象然后放到工廠(chǎng)中去,需要的朋友可以參考下2024-01-01
SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證,其實(shí)創(chuàng)建后端springboot工程也很簡(jiǎn)單,本文安裝idea步驟一步步給大家詳細(xì)介紹,需要的朋友可以參考下2021-09-09
JAVA多線(xiàn)程并發(fā)下的單例模式應(yīng)用
單例模式應(yīng)該是設(shè)計(jì)模式中比較簡(jiǎn)單的一個(gè),也是非常常見(jiàn)的,但是在多線(xiàn)程并發(fā)的環(huán)境下使用卻是不那么簡(jiǎn)單了,今天給大家分享一個(gè)我在開(kāi)發(fā)過(guò)程中遇到的單例模式的應(yīng)用。2017-03-03

