java - 使用扫描仪出现 NoSuchElementException

标签 java exception java.util.scanner nosuchelementexception

有人可以解释一下,为什么在第 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");
                }
            }


        }
    }

最佳答案

您应该在代码中解决以下问题:

  1. 使用 nextLine() 而不是 nextInt()next()。查看 Scanner is skipping nextLine() after using next() or nextFoo()? 了解更多信息。
  2. 尽管不是强制性要求,但每当您请求用户输入时,您都应该打印一条描述输入的消息。
  3. 可能您的要求是存储以姓名为键的联系人,这不是一个好的设计。如果您尝试放置另一个同名联系人,则旧联系人将被新联系人替换,因为 map 会将旧条目替换为具有相同键的新条目。您应该使用唯一键将数据存储在 map 中,在这种情况下,唯一键可以是电话号码和您能想到的其他一些唯一标识符。
  4. 您应该在 while 循环之外请求输入以搜索联系人;否则,系统将提示用户 n 次输入要在通讯录中搜索的姓名。
  5. 在通讯录中找到该姓名后,请务必打破循环。如果循环没有被破坏(即在 map 中找不到该名称),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

关于java - 使用扫描仪出现 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932458/

相关文章:

java - 通过 Socket 进行流传输

java - ClassNotFoundException com.mysql.jdbc.Driver

c++ - 异常在 MSVC 中的析构函数中抛出异常

java - CSVWriter 不允许写入文件

java - 有没有办法使用扫描仪创建一个简单的方程

java - NoSuchMethodError : oracle. forms.handler.IHandler.getApplet()Ljava/applet/Applet

java - netbeans java代码审查插件

java - Android Java 在单独的线程中运行类似于 C# 风格的 Action 列表

java - JFileChooser 中的多种类型选择

Java 与 Sublime Text - 扫描仪无法工作?