第8天:字典和地图JAVA

问题描述 投票:-2回答:2

我正试图解决这个问题 https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem?h_r=next-challenge&h_v=zen 使用链表和ind它在eclipse中成功解决,所有输出都正确,如hackerrank ouputs但当我试图在网站上传我的代码时,它显示运行时错误与InputMismatchException

线程“main”java.util.InputMismatchException中的异常 at java.util.Scanner.throwFor(Scanner.java:909) 在java.util.Scanner.next(Scanner.java:1530) 在java.util.Scanner.nextInt(Scanner.java:2160) 在java.util.Scanner.nextInt(Scanner.java:2119) at Person.main(Person.java:56)

这是我在课堂上的代码

public class Person {

    //Node Structure
    class Node
    {
        public String name;
        public int phone;
        Node next;
    };
    public Node head;

    //Linked List Functions
    public void add(String name , int phone ) //Add To End
    {
        Node n = new Node();
        n.name = name;
        n.phone = phone;
        n.next = head;
        head = n;
    }

    public void search(String name2) //Search inside The List
    {
        Node n = head;
        boolean flag = false;
        while(n != null)
        {
            if(name2.equals(n.name))
            {
                flag = true;
                System.out.println(n.name + "=" + n.phone);
                break;
            }
            n = n.next;
        }
        if(!flag)
            System.out.println("Not found");
    }

    //Main Function
    public static void main(String[] args) {
        //Objects From Classes
        Scanner s = new Scanner(System.in);
        Person p = new Person();
        int n = s.nextInt();

        for(int i=0 ; i<n ; i++)
        {
            s.nextLine();
            String name = s.nextLine();
            int phone = s.nextInt();
            p.add(name, phone);
        }

        s.nextLine();
        while(s.hasNext())
        {
            String name2 = s.next();
            p.search(name2);
            //System.exit(1);
        }

        s.close();
}
java exception inputmismatchexception
2个回答
2
投票

似乎问题出现是因为hackerrank.com期望使用Scaner.next()方法的解决方案:

for(int i = 0; i < n; i++){
        String name = in.next();
        int phone = in.nextInt();
        // Write code here
    }

但你使用Scaner.nextLine()方法:

for(int i=0 ; i<n ; i++)
    {
        s.nextLine();
        String name = s.nextLine();
        int phone = s.nextInt();
        p.add(name, phone);
    }

和测试数据他们用来检查你的解决方案不适合。

尝试重构您的代码以使用Scaner.next()而不是Scaner.nextLine()方法来获取名称值。


1
投票

不确定hackerrank是否存在问题,但在我的系统上运行代码时遇到了同样的错误。有一个InputMismatchException。使用以下代码更改逻辑以接收来自用户的输入:

// Main Function
    public static void main(String[] args) {
        //Objects From Classes
        Scanner s = new Scanner(System.in);
        Person p = new Person();
        int n = s.nextInt();

        // Using a different scanner to take 'n' lines of input
        Scanner ss = new Scanner(System.in);
        for (int i = 0; i < n; i++) {      
            // storing each line
            String line = ss.nextLine();
            // parsing 'line' based on the delimiter " "
            String input[] = line.split(" ");

            String name = input[0];
            int phone = Integer.parseInt(input[1]);
            p.add(name, phone);
        }

        List<String> listOfNames = new ArrayList<>();
        while (n > 0) {
           String name2 = s.next();
           listOfNames.add(name2);
           n--;
        }

        listOfNames.stream().forEach(e -> p.search(e));

        s.close();
        ss.close();
    }
© www.soinside.com 2019 - 2024. All rights reserved.