有人能解释一下吗,为什么在第19行,编译器给我抛出了例外?我只是想不通……我在HackerRank上解决了一些练习,我知道,有解决办法,但是我的代码工作得很好,直到一个测试用例抛出异常。我只是想不通,尽管我读了关于这个的博客文章。
import java.util.*;
import java.io.*;
import java.util.Scanner;
class Solution{
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> contactBook = new HashMap<>();
int n = scanner.nextInt();
scanner.next();
for(int i = 0; i < n; i++) {
String name = scanner.nextLine();
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
}
while(n-- > 0) {
String search = scanner.nextLine();
if(contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
} else {
System.out.println("Not found");
}
}
}
}
您应该在代码中解决以下问题:
nextLine
() 代替 nextInt()
和 next()。
检查扫描仪在使用 next() 或 nextFoo() 后是否跳过 nextLine()?了解更多信息。while
循环之外搜索联系人;否则,系统将提示用户 n
次在通讯录中输入要搜索的姓名。打破
循环。如果循环没有中断(即在映射中找不到名称),n
的值最终将变为 -1
,您可以使用该值打印找不到名称的消息。下面给出的代码包含了上面提到的要点:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 0;
boolean valid;
Map<String, String> contactBook = new HashMap<>();
do {
valid = true;
System.out.print("Enter the number of contacts to be saved: ");
try {
n = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < n; i++) {
System.out.println("---Contact#" + (i + 1) + "---");
System.out.print("Enter the name: ");
String name = scanner.nextLine();
System.out.print("Enter the phone number: ");
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
}
} catch (NumberFormatException e) {
System.out.println("This is an invalid entry. Please try again.");
valid = false;
}
} while (!valid);
System.out.print("Enter the name to serach in the contact book: ");
String search = scanner.nextLine();
while (n-- > 0) {
if (contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
break;
}
}
if (n < 0) {
System.out.println("Not found");
}
}
}
运行示例:
Enter the number of contacts to be saved: 3
---Contact#1---
Enter the name: Arvind
Enter the phone number: 1234567890
---Contact#2---
Enter the name: Kumar
Enter the phone number: 1023456789
---Contact#3---
Enter the name: Avinash
Enter the phone number: 2013456789
Enter the name to serach in the contact book: Kumar
Kumar=1023456789
另一个示例运行:
Enter the number of contacts to be saved: 2
---Contact#1---
Enter the name: Hegyi
Enter the phone number: 1234567890
---Contact#2---
Enter the name: Levente
Enter the phone number: 1023456789
Enter the name to serach in the contact book: Hello
Not found
另一个示例运行:
Enter the number of contacts to be saved: abc
This is an invalid entry. Please try again.
Enter the number of contacts to be saved: 10.5
This is an invalid entry. Please try again.
Enter the number of contacts to be saved: 2
---Contact#1---
Enter the name: Test1
Enter the phone number: 123
---Contact#2---
Enter the name: Test2
Enter the phone number: 234
Enter the name to serach in the contact book: Test2
Test2=234
更改scanner . next();to scanner . nextline();如本链接https://stackoverflow.com/a/24773533/7877099所述
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> contactBook = new HashMap<>();
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
String name = scanner.nextLine();
String phoneNumber = scanner.nextLine();
contactBook.put(name, phoneNumber);
System.out.println(name + " - " + phoneNumber);
}
while (n-- > 0) {
String search = scanner.nextLine();
if (contactBook.containsKey(search)) {
System.out.println(search + "=" + contactBook.get(search));
} else {
System.out.println("Not found");
}
}
}
}