Sort by

recency

|

377 Discussions

|

  • + 0 comments
    //Complete this code or write your own from scratch
    import java.util.*;
    import java.io.*;
    
    class Solution{
    	public static void main(String []argh)
    	{
    		Scanner in = new Scanner(System.in);
            
    		int n = in.nextInt();
            
            if (n < 0 || n > 100000) {
                throw new IllegalArgumentException("The number of people-phones cannot be lower than 0 or greater than 100,000");
            }
    		
            Map<String, Integer> phoneBook = new HashMap<>();
            
            in.nextLine();
            
    		for(int i=0; i<n; i++) {
    			String name = in.nextLine().toLowerCase();
    			Integer phone = in.nextInt();
                in.nextLine();
    			
                if (String.valueOf(phone).length() != 8) {
                    throw new IllegalArgumentException(String.format("The phone number %s is not 8 digits long", phone));
                }
                
                phoneBook.put(name, phone);
    		}
            
    		while(in.hasNext()) {
    			String s = in.nextLine().toLowerCase();
                
                Integer phone = phoneBook.get(s);
                
                if (phone == null) {
                    System.out.println("Not found");
                } else {
                    System.out.println(String.format("%s=%d", s, phone));
                }
    		}
    	}
    }
    
  • + 0 comments

    Here is Java Map solution - https://programmingoneonone.com/hackerrank-java-map-problem-solution.html

  • + 1 comment

    I don't understand the sample testcase. Tom clearly has a phone number why the output is not found??

  • + 0 comments
    Scanner scanner = new Scanner(System.in);
            Map<String, String> phone = new HashMap<>();
            int n = scanner.nextInt();
            scanner.nextLine(); // consume newline after reading int
    
            for (int i = 0; i < n; i++) {
                String name = scanner.nextLine();
                String number = scanner.nextLine();
                phone.put(name, number);
            }
    
            while (scanner.hasNextLine()) {
                String qr = scanner.nextLine();
                if (phone.containsKey(qr)) {
                    System.out.println(qr + "=" + phone.get(qr));
                } else {
                    System.out.println("Not found");
                }
            }
    
            scanner.close();
    
  • + 0 comments
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        int n = Integer.parseInt(br.readLine().trim());
        Map<String, Integer> phoneBook = new HashMap<>();
    
        for(int i=0; i<n; i++){
            String name = br.readLine().trim();
            int phoneNumber = Integer.parseInt(br.readLine().trim());
            phoneBook.put(name, phoneNumber);
        }
    
        String query;
        StringBuilder sb = new StringBuilder();
        while((query = br.readLine())!=null && !query.isEmpty()){
            if(phoneBook.containsKey(query)){
                sb.append(query+"="+phoneBook.get(query)).append("\n");
            } else{
                sb.append("Not found\n");
            }
        }
        System.out.println(sb.toString().trim());
    }
    

    }