NoSuchElementException 使用扫描仪
NoSuchElementException using Scanner
谁能解释一下,为什么在第 19 行编译器抛出异常?我只是想不通...我在 HackerRank 上解决了一些练习,我知道,有解决方法,但我的代码运行完美,直到 1 个测试用例抛出异常。尽管我阅读了有关该问题的博客文章,但我还是想不通。
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");
}
}
}
}
改变scanner.next();到 scanner.next行();如本 link
中所述
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");
}
}
}
}
您应该在代码中解决以下问题:
- 使用
nextLine()
代替 nextInt()
和 next()
。查看 Scanner is skipping nextLine() after using next() or nextFoo()? 了解更多信息。
- 虽然不是强制性要求,但无论何时请求用户输入,都应始终打印一条描述输入的消息。
- 可能你的需求是以姓名为键存储联系人,这不是一个好的设计。如果您尝试放置另一个具有相同名称的联系人,则旧联系人将被新联系人替换,因为地图将旧条目替换为具有相同键的新条目。您应该将数据存储在具有唯一键的映射中,在这种情况下,唯一键可以是 phone 数字和您能想到的其他一些唯一标识符。
- 您应该在
while
循环之外请求输入以搜索联系人;否则,将提示用户 n
次输入要在通讯录中搜索的姓名。
- 在通讯录中找到姓名后,确保
break
循环。如果循环没有中断(即在地图中找不到名称),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
谁能解释一下,为什么在第 19 行编译器抛出异常?我只是想不通...我在 HackerRank 上解决了一些练习,我知道,有解决方法,但我的代码运行完美,直到 1 个测试用例抛出异常。尽管我阅读了有关该问题的博客文章,但我还是想不通。
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");
}
}
}
}
改变scanner.next();到 scanner.next行();如本 link
中所述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");
}
}
}
}
您应该在代码中解决以下问题:
- 使用
nextLine()
代替nextInt()
和next()
。查看 Scanner is skipping nextLine() after using next() or nextFoo()? 了解更多信息。 - 虽然不是强制性要求,但无论何时请求用户输入,都应始终打印一条描述输入的消息。
- 可能你的需求是以姓名为键存储联系人,这不是一个好的设计。如果您尝试放置另一个具有相同名称的联系人,则旧联系人将被新联系人替换,因为地图将旧条目替换为具有相同键的新条目。您应该将数据存储在具有唯一键的映射中,在这种情况下,唯一键可以是 phone 数字和您能想到的其他一些唯一标识符。
- 您应该在
while
循环之外请求输入以搜索联系人;否则,将提示用户n
次输入要在通讯录中搜索的姓名。 - 在通讯录中找到姓名后,确保
break
循环。如果循环没有中断(即在地图中找不到名称),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