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ù)對(duì)輸入的字符串進(jìn)行按空格符分割,確遇到了”奇葩“的問(wèn)題,每次只能得到第一個(gè)字符串。
開(kāi)始以為是split()函數(shù)用錯(cuò)了,查了資料確定無(wú)誤后,覺(jué)得應(yīng)該是輸入的有問(wèn)題。
于是進(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("請(qǐng)輸入第一個(gè)字符串:"); s_nextLine = scan.nextLine(); // 此處使用nextLine(),便于對(duì)比 System.out.println("請(qǐng)輸入第二個(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 + " 長(zhǎng)度: " + s.length()+ '\n'); for(String s : split_nextLine) System.out.println("子串nextLine: "+ count_nextLine++ +": "+ s + " 長(zhǎng)度: " + s.length()+ '\n'); } }
測(cè)試結(jié)果
也驗(yàn)證了我的猜想
注意:
自省,也希望能對(duì)大家有所幫助,少走彎路。
- 用 Scanner 實(shí)現(xiàn)字符串的輸入有兩種方法,一種是next(),一種nextLine();
- next() 一定要讀取到有效字符后才可以結(jié)束輸入,對(duì)輸入有效字符之前遇到的空格鍵、Tab鍵或Enter鍵等結(jié)束符,next() 方法會(huì)自動(dòng)將其去掉,只有在輸入有效字符之后,next()方法才將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結(jié)束符。
- nextLine()方法的結(jié)束符只是Enter鍵。
簡(jiǎn)言之,next方法不能得到帶空格的字符串,而nextLine()方法返回的是Enter鍵之前的所有字符,因此出現(xiàn)了上面測(cè)試樣例的結(jié)果。(ps.一定要注意?。?/p>
Scanner類的next()和nextLine()方法
java的Scanner類可以用來(lái)接收鍵盤輸入的數(shù)據(jù)。next()和nextLine()方法用來(lái)接收字符串,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è)方法一起用可能會(huì)出錯(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()用來(lái)接收回車符,代碼如下:
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)文章
Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05BCryptPasswordEncoder加密與MD5加密的區(qū)別及說(shuō)明
這篇文章主要介紹了BCryptPasswordEncoder加密與MD5加密的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08springboot中nacos-client獲取配置的實(shí)現(xiàn)方法
本文主要介紹了springboot中nacos-client獲取配置的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題
這篇文章主要介紹了解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06mybatis實(shí)現(xiàn)mapper代理模式的方式
本文向大家講解mybatis的mapper代理模式,以根據(jù)ide值查詢單條數(shù)據(jù)為例編寫xml文件,通過(guò)mapper代理的方式進(jìn)行講解增刪改查,分步驟給大家講解的很詳細(xì),對(duì)mybatis mapper代理模式相關(guān)知識(shí)感興趣的朋友一起看看吧2021-06-06