-2

I'm trying to solve this problem
https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem?h_r=next-challenge&h_v=zen
using linked list and ind it's successfully solved in eclipse with all the outputs correct like hackerrank ouputs but when i'm trying to upload my code on the website, it displays a run time error with InputMismatchException

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Person.main(Person.java:56)

that's my code inside the class

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();
}
A.Usama
  • 157
  • 2
  • 8

2 Answers2

2

It seems that issue appears because hackerrank.com expects a solution with using of Scaner.next() method:

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

but you use Scaner.nextLine() method:

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

and test data what they use to check your solution do not fit.

Try to refactor your code to use Scaner.next() instead Scaner.nextLine() method to get a name value.

Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
  • I ran his code on eclipse, I was getting the same issue. Seems to be an issue with the logic and not with *hackerrank*. – Nicholas K Sep 02 '18 at 11:35
1

Not sure if there is an issue with hackerrank, but I faced the same error while running your code on my system. There was an InputMismatchException. Change your logic to receive input from user using the following code :

// 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();
    }
Nicholas K
  • 15,148
  • 7
  • 31
  • 57