Created
December 7, 2015 09:34
-
-
Save ProZhar/753b1ce06f4e13e499b0 to your computer and use it in GitHub Desktop.
com.javarush.test.level10.lesson11.bonus02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.javarush.test.level10.lesson11.bonus02; | |
| import java.io.*; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| /* Нужно добавить в программу новую функциональность | |
| Задача: Программа вводит с клавиатуры пару (число и строку) и выводит их на экран. | |
| Новая задача: Программа вводит с клавиатуры пары (число и строку), сохраняет их в HashMap. | |
| Пустая строка – конец ввода данных. Числа могу повторяться. Строки всегда уникальны. Введенные данные не должны потеряться! | |
| Затем программа выводит содержание HashMap на экран. | |
| Пример ввода: | |
| 1 | |
| Мама | |
| 2 | |
| Рама | |
| 1 | |
| Мыла | |
| Пример вывода: | |
| 1 Мыла | |
| 2 Рама | |
| 1 Мама | |
| */ | |
| public class Solution | |
| { | |
| public static void main(String[] args) throws IOException | |
| { | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
| Map<String, Integer> map = new HashMap<>(); | |
| while (true) { | |
| String num = reader.readLine(); | |
| if (num.isEmpty()) break; | |
| String name = reader.readLine(); | |
| map.put(name, Integer.parseInt(num)); | |
| } | |
| for(Map.Entry<String, Integer> pair: map.entrySet()){ | |
| String key = pair.getKey(); | |
| Integer value = pair.getValue(); | |
| System.out.println(value+" "+key); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment