Sometimes it works and sometimes it doesn't.
I will enter the name of the city A and I will get the family B
I will enter the name of the city of AA and I will get the BB family
I will enter the city name AAA and I will get the BBB family
And sometimes I enter the name of the city A and the program ends.
Example:
Save the City: A
Save the Family: B
Save the City: AA
Save the Family: BB
Save the City: AAA
Save the Family: BBB
Save the City:
By entering the name of the city, you will receive the Family that lives there.
Enter a city name: A
Process finished with exit code 0
package pl.codegym.task.task08.task0829;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
Aktualizacja oprogramowania
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Lista adresów
HashMap<String, String> miasta = new HashMap<>();
while (true) {
System.out.print("Save the City: ");
String miasto = reader.readLine();
if (miasto.isEmpty()) {
break;
}
System.out.print("Save the Family: ");
String rodzina = reader.readLine();
if (rodzina.isEmpty()) {
break;
}
miasta.put(miasto, rodzina);
}
Iterator<HashMap.Entry<String, String>> iterator = miasta.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> para = iterator.next();
String miasto = para.getKey();
String rodzina = para.getValue();
System.out.println("By entering the name of the city, you will receive the Family that lives there.");
System.out.print("Enter a city name: ");
String odczytajMiasto = reader.readLine();
if (odczytajMiasto != null && !odczytajMiasto.isEmpty() && odczytajMiasto.equals(miasto)) {
System.out.println(rodzina);
}
else break;
}
// Wczytuje numer domu
//int numerDomu = Integer.parseInt(reader.readLine());
/*
if (0 <= numerDomu && numerDomu < adresy.size()) {
String nazwiskoRodziny = adresy.get(numerDomu);
System.out.println(nazwiskoRodziny);
}
*/
}
}