I want to write a function that receives two variables of type String representing two IP addresses and a third variable representing a group of IP addresses. The function returns all addresses between IP1 and IP2 from the given set of addresses, taking into account the order.
this is the function signature:
public static String[] get_available_IPs(String ip1, String ip2, String[] addresses)
I tried to split each ip to compare them in the end but I didn't get any result
This is the code that I wrote while i'm trying to solve this problem. I don't think that it will help you to solve the problem but I just want you to take a look of what I did.
String[] ip_numbers1 = ip1.split("\\.");
String[] ip_numbers2 = ip2.split("\\.");
String[][] ip_numbers3 = new String[4][addresses.length];
boolean[] flags = new boolean[4];
for (int i = 0; i < addresses.length; i++) {
for (int j = 0; j < 4; j++) {
String[] new1 = addresses[j].split("\\.");
for (int k = 0; k < 4; k++) {
ip_numbers3[j][k] = new1[k];
}
}
}
for (int i = 0; i < ip_numbers3.length; i++) {
if (Integer.parseInt(ip_numbers1[0]) <= Integer.parseInt(ip_numbers3[i][0])
&& Integer.parseInt(ip_numbers2[0]) >= Integer.parseInt(ip_numbers3[i][0])) {
flags[i] = true;
}
}
sample inputs and outputs:
input:
ip1 = '192.168.1.1'
ip2 = '220.150.1.0'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.2']
output:
['194.200.1.5','193.168.10.20','192.168.1.2']
input:
ip1 = '191.168.1.1'
ip2 = '222.155.1.5'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.1']
output:
['221.155.1.5','194.200.1.5','193.168.10.20','192.168.1.1']