2016-01-19 2 views
-2
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 

class Solution { 
    public static void main(String[] argh) { 
     Map<String, String> phoneBook = new HashMap<String, String>(); 

     Scanner sc = new Scanner(System.in); 
     int t = sc.nextInt(); 
     sc.nextLine(); 
     for (int i = 0; i < t; i++) { 
      String name = sc.nextLine(); 
      String phnum = sc.nextLine(); 

      phoneBook.put(name, phnum); 
      sc.nextLine(); 
     } 

     System.out.println("Not Found"); 
     while (sc.hasNext()) { 
      String s = sc.nextLine(); 

      if (phoneBook.get(s) == null) 
       System.out.println("Not Found"); 
      else 
       System.out.println(s + "=" + phoneBook.get(s)); 
      sc.nextLine(); 
     } 
    } 
} 
+0

Почему вы думаете hasNext не работает? Вы пытались отладить код? – SMA

+0

Просьба уточнить заявление о проблеме, код обмена не помогает нам понять конечную цель. –

+0

hasNext() работает. Любые другие вопросы? –

ответ

2

Попробуйте этот код:

public static void main(String[] argh) { 

    Map<String, String> phoneBook = new HashMap<String, String>(); 

    Scanner sc = new Scanner(System.in); 
    System.out.println(" Enther the Size of Phone book : "); 
    int t = sc.nextInt(); 
    sc.nextLine(); 

    for (int i = 0; i < t; i++) { 
     System.out.println("Enter Name :"); 
     String name = sc.nextLine(); 
     System.out.println("Enter Phone No :"); 
     String phnum = sc.nextLine(); 

     phoneBook.put(name, phnum); 

     // sc.nextLine(); 

    } 
    System.out.println("***Search Phone Numbers (enter Name)***\n"); 
    while (sc.hasNext()) { 

     String s = sc.nextLine(); 

     if (phoneBook.get(s) == null) { 
      System.out.println("Not Found"); 
     } else { 
      System.out.println(s + "=" + phoneBook.get(s)); 
     } 
     //sc.nextLine(); 
    } 
} 
+0

Спасибо! оно работает –