0

The essence of the program is this. You need to measure the response time to the server and write it to a variable. Everything works fine, but if I choose to have the list of available ip addresses read from a file, then the user needs to enter the path and filename. As soon as I start doing this, the compiler skips the lines

path = scr.nextLine(); 
filename = scr.nextLine();

That is, it looks like this, the user enters the number 2, and then immediately

System.out.println("Enter the path to the file"); 
System.out.println("Enter file name");

Even without giving the opportunity to enter the path and name, why is that?

public static void main(String[] args) throws Exception {
    int ipCount;
    byte choiceUser;
    ArrayList<String> ip = new ArrayList<>();
    ArrayList<String> averageTime = new ArrayList<>();
    HashMap<String,Integer> ipAndAverageTime = new HashMap<>();
    Scanner scr = new Scanner(System.in);
    System.out.println("1. Enter ip manually\n2. Read from file");
    choiceUser = scr.nextByte();
    if(choiceUser == 1) {
        System.out.println("Enter the desired number of ip addresses: ");
        ipCount = scr.nextInt();
        System.out.println("Enter " + ipCount + " ip addresses");
        for (int i = 0; i < ipCount; i++) {
            ip.add(scr.next());
            averageTime.add(getAverageTime(ip.get(i)));
            ipAndAverageTime.put(ip.get(i), (int) Double.parseDouble(averageTime.get(i)));
        }
        sortAverageTime(ipAndAverageTime);
        writeToFile(ipAndAverageTime);
        printResult();
    }
    else if (choiceUser == 2) {
        String path;
        String filename;
        System.out.println("Enter file path");
        path = scr.nextLine();
        System.out.println("Enter filename");
        filename = scr.nextLine();
        ip = readFromFile(path, filename);
        for (int i = 0; i < ip.size(); i++) {
            averageTime.add(getAverageTime(ip.get(i)));
            ipAndAverageTime.put(ip.get(i), (int) Double.parseDouble(averageTime.get(i)));
        }
        sortAverageTime(ipAndAverageTime);
        writeToFile(ipAndAverageTime);
        printResult();
    }
}

0 Answers0