I have some questions below code block in Java:
- Output gives 5 times New Delhi. I wonder why the other map elements are not listed and the last one is repeated constantly? The output is:
` City Code: DEL --> City Name: New Delhi
City Code: DEL --> City Name: New Delhi
City Code: DEL --> City Name: New Delhi
City Code: DEL --> City Name: New Delhi
City Code: DEL --> City Name: New Delhi
- For example if the user gives 3 for the first input (for the number of elements), program takes only 2 element to the array then it stops.
- This code block never stops, how can I stop the program?
The code block:
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(5);
    Map<String, City> map = new HashMap<>();
    City moscow = new Moscow("Moscow", "MOW");
    map.put(moscow.getCityCode(), moscow);
    City london = new London("London", "LON");
    map.put(london.getCityCode(), london);
    City newyork = new NewYork("New York", "NYC");
    map.put(newyork.getCityCode(), newyork);
    City berlin = new Berlin("Berlin", "BER");
    map.put(berlin.getCityCode(), berlin);
    City newdelhi = new NewDelhi("New Delhi", "DEL");
    map.put(newdelhi.getCityCode(), newdelhi);
    List<City> cityByCode = new ArrayList<>(map.values());
    Collections.sort(cityByCode);
    for (int i = 0; i < cityByCode.size(); i++) {
        System.out.println("City Code: " + City.getCityCode() + " --> City Name: " + City.getCityName());
    }
    Scanner s = new Scanner(System.in);
    System.out.println("Select a minimum of three and a maximum of five cities listed above. First of all, please specify the number of cities you will choose: ");
    int length = s.nextInt();
    System.out.println("Please enter " + length + " city codes ");
    String[] myArray = new String[length];
    for (int i = 0; i < length; i++) {
        myArray[i] = s.nextLine();
    }
    for (String list : myArray) {
        switch (list) {
            case "MOW":
                executorService.execute(new SimpleThread(moscow));
                break;
            case "LON":
                executorService.execute(new SimpleThread(london));
                break;
            case "NYC":
                executorService.execute(new SimpleThread(newyork));
                break;
            case "BER":
                executorService.execute(new SimpleThread(berlin));
                break;
            case "DEL":
                executorService.execute(new SimpleThread(newdelhi));
                break;
        }
    }
}
}
 
    