java之scan.next()與scan.nextline()函數(shù)的使用及區(qū)別
scan.next()與scan.nextline()函數(shù)的使用及區(qū)別
今天在做牛客網(wǎng)編程練習題“length of last word”時,當編寫實現(xiàn)代碼時,使用split()函數(shù)對輸入的字符串進行按空格符分割,確遇到了”奇葩“的問題,每次只能得到第一個字符串。
開始以為是split()函數(shù)用錯了,查了資料確定無誤后,覺得應(yīng)該是輸入的有問題。
于是進行了下面的實驗:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
String s_next = "";
String s_nextLine = "";
int count_next = 0; // 計數(shù)
int count_nextLine = 0; // 計數(shù)
Scanner scan = new Scanner(System.in);
System.out.println("請輸入第一個字符串:");
s_nextLine = scan.nextLine(); // 此處使用nextLine(),便于對比
System.out.println("請輸入第二個字符串:");
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é)果
也驗證了我的猜想

注意:
自省,也希望能對大家有所幫助,少走彎路。
- 用 Scanner 實現(xiàn)字符串的輸入有兩種方法,一種是next(),一種nextLine();
- next() 一定要讀取到有效字符后才可以結(jié)束輸入,對輸入有效字符之前遇到的空格鍵、Tab鍵或Enter鍵等結(jié)束符,next() 方法會自動將其去掉,只有在輸入有效字符之后,next()方法才將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結(jié)束符。
- nextLine()方法的結(jié)束符只是Enter鍵。
簡言之,next方法不能得到帶空格的字符串,而nextLine()方法返回的是Enter鍵之前的所有字符,因此出現(xiàn)了上面測試樣例的結(jié)果。(ps.一定要注意!)
Scanner類的next()和nextLine()方法
java的Scanner類可以用來接收鍵盤輸入的數(shù)據(jù)。next()和nextLine()方法用來接收字符串,next()方法接收字符串時遇到空格或回車結(jié)束輸入,而nextLine()方法可以接收空格,最后輸入回車才結(jié)束。下面用實例演示
兩者的區(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);
}
}
運行結(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);
}
}
運行結(jié)果截圖:

兩個方法一起用可能會出錯:
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);
}
}
運行結(jié)果截圖:

這時程序已結(jié)束運行,不能再輸入。原因是next()方法遇到回車結(jié)束輸入,卻把最后的回車符留給了nextLine(),nextLine()方法接收了一個空字符串。
解決方法是next()方法后面再加一個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);
}
}
運行結(jié)果截圖:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下2016-05-05
BCryptPasswordEncoder加密與MD5加密的區(qū)別及說明
這篇文章主要介紹了BCryptPasswordEncoder加密與MD5加密的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
springboot中nacos-client獲取配置的實現(xiàn)方法
本文主要介紹了springboot中nacos-client獲取配置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
解決BeanUtils.copyProperties無法成功封裝的問題
這篇文章主要介紹了解決BeanUtils.copyProperties無法成功封裝的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

