第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在輸入中使用空格時(shí) Java 掃描器輸入不匹配

在輸入中使用空格時(shí) Java 掃描器輸入不匹配

手掌心 2023-08-04 18:55:30
我正在 java 中使用掃描儀,并嘗試在選項(xiàng) 2 的輸入中輸入一個(gè)空格(從哈希圖中刪除用戶),但是當(dāng)我在答案中添加空格時(shí),我收到一個(gè) InputMismatchException。在研究時(shí),我遇到了這個(gè)線程Scanner Class InputMismatchException and warnings,它說使用這行代碼來解決問題:.useDelimiter(System.getProperty("line.separator"));我已經(jīng)添加了這個(gè),現(xiàn)在我的選項(xiàng) 2 進(jìn)入了我輸入數(shù)據(jù)的永無休止的循環(huán)。這是我的代碼:public class Test {? ? public static void main(String[] args) {? ? ? ? Scanner scan = new Scanner (System.in);? ? ? ? AddressBook ad1 = new AddressBook();? ? ? ? String firstName="";? ? ? ? String lastName="";? ? ? ? String key="";? ? ? ? int choice=0;? ? ? do{? ? ? ? System.out.println("********************************************************************************");? ? ? ? System.out.println("Welcome to the Address book. Please pick from the options below.\n");? ? ? ? System.out.println("1.Add user \n2.Remove user \n3.Edit user \n4.List Contact \n5.Sort contacts \n6.Exit");? ? ? ? ? System.out.print("Please enter a choice: ");? ? ? ? ?choice = scan.nextInt();? ? ? ? if(choice==1){? ? ? ? ? ? //Add user? ? ? ? ? ? System.out.print("Please enter firstname: ");? ? ? ? ? ? firstName=scan.next();? ? ? ? ? ? System.out.print("Please enter lastname: ");? ? ? ? ? ? lastName=scan.next();? ? ? ? ? ? Address address = new Address();? ? ? ? ? ? key = lastName.concat(firstName);? ? ? ? ? ? Person person = new Person(firstName,lastName);? ? ? ? ? ? ad1.addContact(key,person);? ? ? ? ? ? System.out.println("key: " + key);? ? ? ? }? ? ? ? else if(choice==2){? ? ? ? ? ? //Remove user? ? ? ? ? ? System.out.println("Please enter name of user to remove: ");? ? ? ? ? ? scan.useDelimiter(System.getProperty("line.separator"));? ? ? ? ? ? key=scan.next();? ? ? ? ? ? System.out.println("name:" + key);? ? ? ? ? ? ad1.removeContact(key);??? ? ? ? }? ? ? ? else if(choice==3){? ? ? ? ? ? //Edit user? ? ? ? }? ? ? ? else if(choice==4){? ? ? ? ? ? //List contact? ? ? ? ? ? ad1.listAllContacts();? ? ? ? }我需要使用空格的原因是從我的哈希圖中刪除用戶,我需要輸入他們的全名,因?yàn)槊荑€是他們的姓氏和名字的串聯(lián),任何幫助將不勝感激
查看完整描述

1 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

nextInt()行為類似于next()當(dāng)它讀取一行時(shí),它將光標(biāo)放在該行后面。


示例:您輸入 6


 ^(scanner's cursor)

所以下次你打電話的時(shí)候nextLine()。它將返回光標(biāo)之后的整行,在本例中光標(biāo)為空。


要解決此問題,您需要調(diào)用一個(gè)額外函數(shù),nextLine()以便掃描儀關(guān)閉它正在讀取的上一行并轉(zhuǎn)到下一行。


你可以這樣做


System.out.print("Please enter a choice: ");

choice = scan.nextInt(); // Reads the int

scan.nextLine(); // Discards the line

在選擇 2 中,由于您想要用戶的全名,因此您可以使用它nextLine()來獲取整行和空格。


//Remove user

System.out.println("Please enter full name of user to remove: ");

key=scan.nextLine();

System.out.println("name:" + key);

ad1.removeContact(key);  

或者您可以執(zhí)行與選項(xiàng) 1 中類似的操作


System.out.print("Please enter firstname: ");

firstName=scan.next();

System.out.print("Please enter lastname: ");

lastName=scan.next();

key = lastName.concat(firstName);

System.out.println("name:" + key);

ad1.removeContact(key);  

scan.nextLine(); // This is will make sure that in you next loop `nextInt()` won't give an input mismatch exception



查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)