java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別
scan.next()與scan.nextline()函數(shù)的使用及區(qū)別
今天在做??途W(wǎng)編程練習(xí)題“length of last word”時(shí),當(dāng)編寫實(shí)現(xiàn)代碼時(shí),使用split()函數(shù)對輸入的字符串進(jìn)行按空格符分割,確遇到了”奇葩“的問題,每次只能得到第一個(gè)字符串。
開始以為是split()函數(shù)用錯(cuò)了,查了資料確定無誤后,覺得應(yīng)該是輸入的有問題。
于是進(jìn)行了下面的實(shí)驗(yàn):
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
String s_next = "";
String s_nextLine = "";
int count_next = 0; // 計(jì)數(shù)
int count_nextLine = 0; // 計(jì)數(shù)
Scanner scan = new Scanner(System.in);
System.out.println("請輸入第一個(gè)字符串:");
s_nextLine = scan.nextLine(); // 此處使用nextLine(),便于對比
System.out.println("請輸入第二個(gè)字符串:");
s_next = scan.next(); // 第一次使用的next();
scan.close();
String [] split_next = s_next.split("\\s+");
String [] split_nextLine = s_nextLine.split("\\s+");
for(String s : split_next)
System.out.println("子串next: "+ count_next++ +": "+ s + " 長度: " + s.length()+ '\n');
for(String s : split_nextLine)
System.out.println("子串nextLine: "+ count_nextLine++ +": "+ s + " 長度: " + s.length()+ '\n');
}
}
測試結(jié)果
也驗(yàn)證了我的猜想

注意:
自省,也希望能對大家有所幫助,少走彎路。
- 用 Scanner 實(shí)現(xiàn)字符串的輸入有兩種方法,一種是next(),一種nextLine();
- next() 一定要讀取到有效字符后才可以結(jié)束輸入,對輸入有效字符之前遇到的空格鍵、Tab鍵或Enter鍵等結(jié)束符,next() 方法會自動將其去掉,只有在輸入有效字符之后,next()方法才將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結(jié)束符。
- nextLine()方法的結(jié)束符只是Enter鍵。
簡言之,next方法不能得到帶空格的字符串,而nextLine()方法返回的是Enter鍵之前的所有字符,因此出現(xiàn)了上面測試樣例的結(jié)果。(ps.一定要注意?。?/p>
Scanner類的next()和nextLine()方法
java的Scanner類可以用來接收鍵盤輸入的數(shù)據(jù)。next()和nextLine()方法用來接收字符串,next()方法接收字符串時(shí)遇到空格或回車結(jié)束輸入,而nextLine()方法可以接收空格,最后輸入回車才結(jié)束。下面用實(shí)例演示
兩者的區(qū)別:
next()方法
package scanner;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String a,b;
Scanner sc=new Scanner(System.in);
System.out.println("next()方法接收字符串:");
a=sc.next();
System.out.println(a);
}
}
運(yùn)行結(jié)果截圖:

nextLine()方法
package scanner;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String a,b;
Scanner sc=new Scanner(System.in);
System.out.println("nextLine()方法接收字符串:");
b=sc.nextLine();
System.out.println(b);
}
}
運(yùn)行結(jié)果截圖:

兩個(gè)方法一起用可能會出錯(cuò):
package scanner;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String a,b;
Scanner sc=new Scanner(System.in);
System.out.println("next()方法接收字符串:");
a=sc.next();
System.out.println(a);
System.out.println("nextLine()方法接收字符串:");
b=sc.nextLine();
System.out.println(b);
}
}
運(yùn)行結(jié)果截圖:

這時(shí)程序已結(jié)束運(yùn)行,不能再輸入。原因是next()方法遇到回車結(jié)束輸入,卻把最后的回車符留給了nextLine(),nextLine()方法接收了一個(gè)空字符串。
解決方法是next()方法后面再加一個(gè)nextLine()用來接收回車符,代碼如下:
package scanner;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String a,b;
Scanner sc=new Scanner(System.in);
System.out.println("next()方法接收字符串:");
a=sc.next();
System.out.println(a);
a=sc.nextLine();//接收回車符
System.out.println("nextLine()方法接收字符串:");
b=sc.nextLine();
System.out.println(b);
}
}
運(yùn)行結(jié)果截圖:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jvm調(diào)優(yōu)的幾種場景(小結(jié))
本文主要介紹了jvm調(diào)優(yōu)的幾種場景,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法
Spring Boot Actuator 是開發(fā)和管理生產(chǎn)級 Spring Boot 應(yīng)用程序的重要工具,它可以幫助你確保應(yīng)用程序的穩(wěn)定性和性能,本文給大家介紹了SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法,需要的朋友可以參考下2024-05-05
詳解java并發(fā)編程(2) --Synchronized與Volatile區(qū)別
這篇文章主要介紹了Synchronized與Volatile區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
SpringBoot配置多個(gè)數(shù)據(jù)源超簡單步驟(連接多個(gè)數(shù)據(jù)庫)
公司項(xiàng)目有連接多個(gè)不同數(shù)據(jù)庫的需求,特研究了一下,根據(jù)網(wǎng)上的資料,這篇文章主要給大家介紹了關(guān)于SpringBoot配置多個(gè)數(shù)據(jù)源(連接多個(gè)數(shù)據(jù)庫)的相關(guān)資料,需要的朋友可以參考下2024-05-05
淺談Java自定義注解和運(yùn)行時(shí)靠反射獲取注解
下面小編就為大家?guī)硪黄獪\談Java自定義注解和運(yùn)行時(shí)靠反射獲取注解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
spring session同域下單點(diǎn)登錄實(shí)現(xiàn)解析
這篇文章主要介紹了spring session同域下單點(diǎn)登錄實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

