Java Scanner如何獲取字符串和帶空格的字符串
Scanner獲取字符串和帶空格的字符串
next() 針對不帶空格的字符串
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("地址:");
String host = s.next();
System.out.println("host = " + host);
System.out.print("端口:");
String port = s.next();
System.out.println("port = " + port);
System.out.print("用戶名:");
String user = s.next();
System.out.println("user = " + user);
System.out.print("帶空格的用戶名:");
String userSpace = s.next();
System.out.println("userSpace = " + userSpace);
}
nextLine()針對帶空格的字符串
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("地址:");
String host = s.nextLine();
System.out.println("host = " + host);
System.out.print("端口:");
String port = s.nextLine();
System.out.println("port = " + port);
System.out.print("用戶名:");
String user = s.nextLine();
System.out.println("user = " + user);
System.out.print("帶空格的用戶名:");
String userSpace = s.nextLine();
System.out.println("userSpace = " + userSpace);
}
Scanner類——獲取用戶輸入的字符串
注意區(qū)分Scanner類中的獲取輸入字符串的兩種方法:
next() 和 nextLine()
| next() | nextLine() |
|---|---|
| 一定要讀取到有效字符后才可以結(jié)束輸入 | 以Enter回車鍵作為結(jié)束符 |
| 有效字符前的空格會自動忽略 | 返回輸入回車之前的所有字符 |
| 有效字符后的空格會被作為結(jié)束符 | 可以獲得空白字符串 |
| 綜上,next()不能得到帶有空格的字符串 | 綜上,nextLine()可以得到有空格的字符串 |
使用next()
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
//創(chuàng)建一個Scanner類的對象,準(zhǔn)備從鍵盤接收數(shù)據(jù)
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
if (scanner.hasNext()) {
//scanner.hasNext() 判斷用戶是否還有輸入
String str=scanner.next(); //如果還有輸入,通過scanner.next()接收用戶的輸入
String str=scanner.next();
System.out.println("輸出內(nèi)容:"+str);
}
scanner.close();//屬于I/O流的類,使用結(jié)束后及時關(guān)閉,否則將一直占用資源
}
}示例:

使用nextLine()
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("輸出內(nèi)容:"+str);
}
scanner.close();
}
}示例:

常用nextLine()進(jìn)行字符串的獲取,寫法如下:
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str=scanner.nextLine();
//接收用戶輸入,直至敲下回車結(jié)束,將輸入保存為字符串
System.out.println("輸出的內(nèi)容:"+str);
scanner.close();
}
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java反應(yīng)式框架Reactor中的Mono和Flux
這篇文章主要介紹了Java反應(yīng)式框架Reactor中的Mono和Flux,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
一文教會你如何搭建vue+springboot項(xiàng)目
最近在搗鼓?SpringBoot?與?Vue?整合的項(xiàng)目,所以下面這篇文章主要給大家介紹了關(guān)于如何通過一篇文章教會你搭建vue+springboot項(xiàng)目,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
利用Java獲取被nginx代理的emqx客戶端真實(shí)ip
使用nginx作為負(fù)載均衡(Load Balancing)的時候,發(fā)現(xiàn)真實(shí)ip無法獲取,所以本文小編就來和大家介紹一下如何使用Java獲取被nginx代理的emqx客戶端真實(shí)ip地址吧2025-08-08
Springboot啟用多個監(jiān)聽端口代碼實(shí)例
這篇文章主要介紹了Springboot啟用多個監(jiān)聽端口代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Java利用EasyExcel實(shí)現(xiàn)模板讀取和復(fù)雜表格填充
這篇文章主要為大家詳細(xì)介紹了Java利用EasyExcel實(shí)現(xiàn)模板讀取和復(fù)雜表格填充的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-12-12
Spring常用注解及自定義Filter的實(shí)現(xiàn)
這篇文章主要介紹了Spring常用注解及自定義Filter的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Mybatis查詢返回Map<String,Object>類型實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Mybatis查詢返回Map<String,Object>類型的相關(guān)資料,平時沒太注意怎么用,今天又遇到了總結(jié)記錄一下,方便以后處理此類問題,需要的朋友可以參考下2022-07-07

