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ù)。
*
* 前面的時候:
* A:導(dǎo)包
* B:創(chuàng)建對象
* C:調(diào)用方法
*
* 分析理解:Scanner sc = new Scanner(System.in);
* System類下有一個靜態(tài)的字段:
* public static final InputStream in; 標(biāo)準(zhǔn)的輸入流,對應(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é)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java 定時器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
關(guān)于request.getRequestDispatcher().forward()的妙用及DispatcherType
這篇文章主要介紹了關(guān)于request.getRequestDispatcher().forward()的妙用及DispatcherType對Filter配置的影響,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
IDEA打開java項(xiàng)目后里面的java文件不能運(yùn)行解決辦法
這篇文章主要給大家介紹了關(guān)于IDEA打開java項(xiàng)目后里面的java文件不能運(yùn)行的解決辦法,有時候想運(yùn)行別人的項(xiàng)目,但是別人的項(xiàng)目并非IDEA項(xiàng)目(甚至只有源碼),當(dāng)我們打開項(xiàng)目時候,并不能運(yùn)行,需要的朋友可以參考下2023-10-10
Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解
這篇文章主要介紹了Spring框架中ImportBeanDefinitionRegistrar的應(yīng)用詳解,如果實(shí)現(xiàn)了ImportSelector接口,在配置類中被@Import加入到Spring容器中以后,Spring容器就會把ImportSelector接口方法返回的字符串?dāng)?shù)組中的類new出來對象然后放到工廠中去,需要的朋友可以參考下2024-01-01
SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證,其實(shí)創(chuàng)建后端springboot工程也很簡單,本文安裝idea步驟一步步給大家詳細(xì)介紹,需要的朋友可以參考下2021-09-09

