3

I was solving HackerRank "30 Days Of Code" problem 8.

This is the code I wrote:

import java.util.*;
import java.io.*;
public class Directory
{
    public static void main(String args[])throws NoSuchElementException
    {
        Scanner sc=new Scanner(System.in);
        //System.out.println("Enter number");
        int n=sc.nextInt();
        sc.nextLine();
        Map<String,String> Directory= new HashMap<String,String>();
        for (int i=0;i<n;i++)
        {
         //System.out.println("Enter name and phone number");
         String name=sc.next();
         String ph=sc.next();
         sc.nextLine();
         Directory.put(name,ph);
        }
         while(sc.hasNext())
         {
         String s = sc.next();
         sc.nextLine();
         String phoneNumber = Directory.get(s);
         System.out.println((phoneNumber != null) ? s + "=" + phoneNumber : "Not found");
        }

    }
}

When I run this code with the custom input I get an error as follows:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585) at Directory.main(Directory.java:23)

I think this is occurring due to the "sc.nextLine()" in the while loop. But I'm not able to figure out why. I had learnt from here that I should use sc.nextLine() after using sc.next() so that the control is transferred to the next line of input. Any ideas where I am going wrong?

  • I think it is the `sc.nextLine()` right after the `int n=sc.nextInt();`. – Stephan Strate Jul 22 '17 at 19:23
  • what is the use of `sc.nextLine();`? You're not storing it's return anywhere. – Raman Sahasi Jul 22 '17 at 19:27
  • @RamanSahasi It is to consume the *newline* character during input (which is occuring in different lines). As given [here](https://stackoverflow.com/a/13102066/7313260). –  Jul 22 '17 at 19:29

2 Answers2

3

I think this will work for you. Try it :) You do not need this line sc.nextLine();

public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number");
        int n=sc.nextInt();
        Map<String,String> directory= new HashMap<>();
        for (int i=0;i<n;i++)
        {
            System.out.println("Enter name and phone number");
            String name=sc.next();
            String ph=sc.next();
            sc.nextLine();
            directory.put(name,ph);
        }

        while(sc.hasNext())
        {
            String s = sc.next();
            sc.nextLine();
            String phoneNumber = directory.get(s);
            System.out.println((phoneNumber != null) ? s + "=" + phoneNumber : "Not found");
        }

    }
2

From Documentation:

public String next() Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern

And also from the Documentation of Scanner:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace

And whitespace includes \n and \r.

So as a conclusion, using nextLine() to workaround the newline problem is valid in case of nextInt() but it is not in case of using next().

Yahya
  • 9,370
  • 4
  • 26
  • 43