IP generator
Hi all, I wonder if someone could help me. Im trying to create an IP generator using a for loop. So say i have this private IP - 192.168.0.0. anytime the for loop is executed the IP address increases ie to 192.168.0.1, 192.168.0.2...to 192.168.0.n where n is any number. code with comments will be highly appreciated thanks in advance.
my thoughts;
.
.
.
for (String ip = 0; ip < n; ip++)
{
append n to 192.168.0.0;
//ie a value n is appended to 192.168.0...anytime to loop is executed so we have .1, .2 ,.3 etc...
.
.
}
//honestly not sure what im doin. please help.
[628 byte] By [
fresher] at [2007-11-11 10:05:53]

# 1 Re: IP generator
how many ip's do you want to generate?
String ip = "192.168.0.";
for(int i = 0; i < 10; i++) // go from numbers 0-9
{
ip += i;
System.out.println(ip);
ip = "192.168.0.";
}
anubis at 2007-11-11 22:32:01 >

# 2 Re: IP generator
thanks anubis. much obliged
but i really dont understand how the program works i.e. how the last digit is appended to 192.168.0. as the for loop executes. would be great if u could explain. thanks in advance.
# 3 Re: IP generator
sure
in the line ip += i;
all thats doing is taking "192.168.0." and then concatenating the i value to the end of that. so thats how you get each ip value.
you have to reinitialize ip to 192.168.0. at the end of each iteration or else the numbers will add on to each other. so like the second iteration would be 192.168.0.01 instead of 192.168.0.1
hopefully that was somewhat clear lol
anubis at 2007-11-11 22:34:05 >

# 4 Re: IP generator
sure
in the line ip += i;
all thats doing is taking "192.168.0." and then concatenating the i value to the end of that. so thats how you get each ip value.
you have to reinitialize ip to 192.168.0. at the end of each iteration or else the numbers will add on to each other. so like the second iteration would be 192.168.0.01 instead of 192.168.0.1
hopefully that was somewhat clear lol
very well explained. happy new year.
thanks
# 5 Re: IP generator
how many ip's do you want to generate?
String ip = "192.168.0.";
for(int i = 0; i < 10; i++) // go from numbers 0-9
{
ip += i;
System.out.println(ip);
ip = "192.168.0.";
}
There is a simpler way to do this (As simple as this already is):
String ip = "192.168.0.";
for(int i = 0; i < 10; i++) {
System.out.println(ip + i);
}
With Strings you can use the + operator to append stuff to the end of Strings