Java Program to keep track of a list of Static IP Addresses


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Java Program to keep track of a list of Static IP Addresses

  1. #1
    Join Date
    Oct 2005
    Posts
    10

    Java Program to keep track of a list of Static IP Addresses

    Hello all-

    Basically, I have a bunch of static IP addresses. I have an array filled with the last 3 digits of each address (the first 9 numbers are all the same of the address), and I am going to compare that array to another array filled with the numbers 1 --> length (user defines this). Eventually, I hope to store the IP's being used in a text file rather than an array. Anyways, those two arrays are compared and the IP's not already in the original array will be put into a new one.

    I know, this is confusing, but please look at the code, its alot simpler. It compiles, but I am getting a buffer overflow. Can anyone offer help? Thanks!

    Sorry about the code, I can never get these code boxes to format correctly.

    Code:
       import java.util.Scanner;
    
        public class Free_IP {
       
           public static void main(String[] args) {
           
             int count_Used = 0;										// Keeps track of the # of Used IP Addresses
             int count_New = 0;										//Keeps track of the # of New IP Addresses
          
             Scanner scan = new Scanner(System.in);
          
             System.out.print("Please enter the maximum # of IP Addresses: ");
             int length = scan.nextInt(); 
          
          
          //=======================================================================================================================================================
          // array_Used holds the last digits of the IP Addresses that are already in use
          // array_Sample is an array that holds the numbers 0 --> length, used to compare with array_Used
          // array_New is where the IP Addresses not in use will be stored
          	
             int[] array_Used = {1,13,14,21,21,21,22,22,22,22,27,28,30,33,38,39,51,52,62,63,73,81,89,99,101,104,124,154,156,189,199,201,213,229,235,251,253};
             int[] array_Sample = new int[length];
             int[] array_New = new int[length];
          	
          //=======================================================================================================================================================
          
          
             for (int i=0; i<length; i++){							// While i is less than whatever the user specifies,
             
                array_Sample[i] = i;  								// Set array_Sample[i]=i (Not sure if this works, i want it go initialize
             																// the array to go from 0,1,2,3,4,...length
             }
          
             for (int a = 0; a <= length; a++){					// While a is less than whatever the user specifies,
             
                if (array_Used[a] == array_Sample[a]){			// If the current element in the Used Array = the current element in the Sample array,
                   count_Used++;										// Then just increment count_Used
                
                }
                
                else{														// If the current element in the Used Array is not equal to the current element in the Sample array,
                   
                   for (int c = 0; c<= length; c++){																	
                      array_New[c] = array_Sample[a];			// Then the current element is stored in the New Array
                      count_New++;	
                   														// And count_New is incremented
                   }														
                }
             }
      		//=======================================================================================================================================================    
    		// The following outputs the Free IP Addresses, and count_Used as well as count_New
             for (int d = 0; d <= array_New.length; d++){
             
                System.out.println("The following IP Addresses are free to use:"); 
                System.out.println("XXX.XXX.XXX." + array_New[d]);
                System.out.println("There are " + count_Used + "IP Addresses in use and " + count_New + "IP Addresses that are free."); 
             }
    		//=======================================================================================================================================================
          
          }
       }

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    In the line:
    for(int a = 0; a <= length; a++ )
    That line is wrong, because array_Sample only has indicies from 0 to length-1.

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Oct 2005
    Posts
    10
    i appreciate the help, and i see what you mean, however i am still getting overflow problems

    any more suggestions?

  4. #4
    Join Date
    Aug 2003
    Posts
    313
    You have that a lot of places, did you fix them all? Pretty much anyplace that you use <= you should probably be using <. If you are still getting them, can you post the line number that's throwing the exception?

    Hope this helps.
    ~evlich

  5. #5
    Join Date
    Oct 2005
    Posts
    10
    In an attempt to "visualize" my program more clearly, I added methods to it...unfortunately, I am still getting an overflow, and I can't see why for the life of me...I did what you said and changed all of the <='s to <'s, but it is still not working...I am getting exceptions at:

    line 47: if (array_Used[m] == array_Sample[m]){
    and
    line 21: Compare_Arrays(array_Used, array_Sample, array_New);

    Also, when initializing the arrays, i set the size from length to length+1, this way if the user enters the length as 50, the array contains 0-50 as opposed to 0-49

    thanks for all of your help!

    Code:
     
    import java.util.Scanner;
    	
        public class Free_IP2 {
        
          public static int count_New = 0;
          public static int count_Used = 0;
       
           public static void main(String[] args) {
          
             Scanner scan = new Scanner(System.in);
          
             int[] array_Used = {1,13,14,21,21,21,22,22,22,22,27,28,30,33,38,39,51,52,62,63,73,81,89,99,101,104,124,154,156,189,199,201,213,229,235,251,253};
          
             System.out.print("Please enter the maximum # of IP Addresses " + "[> " + array_Used.length + "]:");
             int length = scan.nextInt();     
            
             int[] array_Sample = new int[length+1];
             int[] array_New = new int[length+1];
          	
             Initialize_Array(array_Sample, length);
             Compare_Arrays(array_Used, array_Sample, array_New);
          	
             for (int d = 0; d < array_New.length; d++){
             
                System.out.println("XXX.XXX.XXX." + array_New[d]);
             	
             }
          	
          }
       	
           public static int[] Initialize_Array(int[] array_Sample, int length){
          
             for (int i=0; i<length+1; i++){							
             
                array_Sample[i] = i;  								
             															
             }    
          	
             return array_Sample;
          
          }
       	
           public static int[] Compare_Arrays(int[] array_Used, int[] array_Sample, int[] array_New){
          
             for (int m=0; m<array_Sample.length; m++){
             
                if (array_Used[m] == array_Sample[m]){
                
                   Free_IP2.count_Used++;
                
                }
                else{
                
                   array_New[m] = array_Sample[m];
    					Free_IP2.count_New++;
                
                }
             
             }
             return array_New;
          }
       	
       	
       }

  6. #6
    Join Date
    Oct 2004
    Posts
    311
    Ah, I think I see the problem. You are using the following line of code:
    if (array_Used[m] == array_Sample[m])
    but what happens when array_Used is smaller then array_Sample?

    I think that changing the compare_arrays method as follows would fix it:

    Code:
    public static int[] Compare_Arrays(int[] array_Used, int[] array_Sample, int[] array_New){
          
             for (int m=0; m<array_Sample.length; m++){
                for(int n=0; n<array_Used; n++) {
                   if (array_Used[n] == array_Sample[m]){
                      Free_IP2.count_Used++;
                   }
                   else{
                      array_New[m] = array_Sample[m];
    	          Free_IP2.count_New++;
                   }
                }
             }
             return array_New;
          }
    What I do here is loop through the Array_Used array once for each item in the ArrayNew array.

    Mark

  7. #7
    Join Date
    Oct 2005
    Posts
    10
    you're right, that did the job, thanks!

    onto the next challenge, however...(there always seems to be more!)

    this is the output that i am getting:

    Code:
    XXX.XXX.XXX.0
    XXX.XXX.XXX.1
    XXX.XXX.XXX.2
    XXX.XXX.XXX.3
    XXX.XXX.XXX.4
    XXX.XXX.XXX.5
    XXX.XXX.XXX.6
    XXX.XXX.XXX.7
    XXX.XXX.XXX.8
    XXX.XXX.XXX.9
    XXX.XXX.XXX.10
    XXX.XXX.XXX.11
    XXX.XXX.XXX.12
    XXX.XXX.XXX.13
    XXX.XXX.XXX.14
    XXX.XXX.XXX.15
    XXX.XXX.XXX.16
    XXX.XXX.XXX.17
    XXX.XXX.XXX.18
    XXX.XXX.XXX.19
    XXX.XXX.XXX.20
    XXX.XXX.XXX.21
    XXX.XXX.XXX.22
    XXX.XXX.XXX.23
    XXX.XXX.XXX.24
    XXX.XXX.XXX.25
    XXX.XXX.XXX.26
    XXX.XXX.XXX.27
    XXX.XXX.XXX.28
    XXX.XXX.XXX.29
    XXX.XXX.XXX.30
    XXX.XXX.XXX.31
    XXX.XXX.XXX.32
    XXX.XXX.XXX.33
    XXX.XXX.XXX.34
    XXX.XXX.XXX.35
    XXX.XXX.XXX.36
    XXX.XXX.XXX.37
    XXX.XXX.XXX.38
    XXX.XXX.XXX.39
    XXX.XXX.XXX.40
    XXX.XXX.XXX.41
    XXX.XXX.XXX.42
    XXX.XXX.XXX.43
    XXX.XXX.XXX.44
    XXX.XXX.XXX.45
    XXX.XXX.XXX.46
    XXX.XXX.XXX.47
    XXX.XXX.XXX.48
    XXX.XXX.XXX.49
    XXX.XXX.XXX.50
    XXX.XXX.XXX.51
    XXX.XXX.XXX.52
    XXX.XXX.XXX.53
    XXX.XXX.XXX.54
    XXX.XXX.XXX.55
    XXX.XXX.XXX.56
    XXX.XXX.XXX.57
    XXX.XXX.XXX.58
    XXX.XXX.XXX.59
    XXX.XXX.XXX.60
    XXX.XXX.XXX.61
    XXX.XXX.XXX.62
    XXX.XXX.XXX.63
    XXX.XXX.XXX.64
    XXX.XXX.XXX.65
    XXX.XXX.XXX.66
    XXX.XXX.XXX.67
    XXX.XXX.XXX.68
    XXX.XXX.XXX.69
    XXX.XXX.XXX.70
    XXX.XXX.XXX.71
    XXX.XXX.XXX.72
    XXX.XXX.XXX.73
    XXX.XXX.XXX.74
    XXX.XXX.XXX.75
    XXX.XXX.XXX.76
    XXX.XXX.XXX.77
    XXX.XXX.XXX.78
    XXX.XXX.XXX.79
    XXX.XXX.XXX.80
    XXX.XXX.XXX.81
    XXX.XXX.XXX.82
    XXX.XXX.XXX.83
    XXX.XXX.XXX.84
    XXX.XXX.XXX.85
    XXX.XXX.XXX.86
    XXX.XXX.XXX.87
    XXX.XXX.XXX.88
    XXX.XXX.XXX.89
    XXX.XXX.XXX.90
    XXX.XXX.XXX.91
    XXX.XXX.XXX.92
    XXX.XXX.XXX.93
    XXX.XXX.XXX.94
    XXX.XXX.XXX.95
    XXX.XXX.XXX.96
    XXX.XXX.XXX.97
    XXX.XXX.XXX.98
    XXX.XXX.XXX.99
    XXX.XXX.XXX.100
    XXX.XXX.XXX.101
    XXX.XXX.XXX.102
    XXX.XXX.XXX.103
    XXX.XXX.XXX.104
    XXX.XXX.XXX.105
    XXX.XXX.XXX.106
    XXX.XXX.XXX.107
    XXX.XXX.XXX.108
    XXX.XXX.XXX.109
    XXX.XXX.XXX.110
    XXX.XXX.XXX.111
    XXX.XXX.XXX.112
    XXX.XXX.XXX.113
    XXX.XXX.XXX.114
    XXX.XXX.XXX.115
    XXX.XXX.XXX.116
    XXX.XXX.XXX.117
    XXX.XXX.XXX.118
    XXX.XXX.XXX.119
    XXX.XXX.XXX.120
    XXX.XXX.XXX.121
    XXX.XXX.XXX.122
    XXX.XXX.XXX.123
    XXX.XXX.XXX.124
    XXX.XXX.XXX.125
    XXX.XXX.XXX.126
    XXX.XXX.XXX.127
    XXX.XXX.XXX.128
    XXX.XXX.XXX.129
    XXX.XXX.XXX.130
    XXX.XXX.XXX.131
    XXX.XXX.XXX.132
    XXX.XXX.XXX.133
    XXX.XXX.XXX.134
    XXX.XXX.XXX.135
    XXX.XXX.XXX.136
    XXX.XXX.XXX.137
    XXX.XXX.XXX.138
    XXX.XXX.XXX.139
    XXX.XXX.XXX.140
    XXX.XXX.XXX.141
    XXX.XXX.XXX.142
    XXX.XXX.XXX.143
    XXX.XXX.XXX.144
    XXX.XXX.XXX.145
    XXX.XXX.XXX.146
    XXX.XXX.XXX.147
    XXX.XXX.XXX.148
    XXX.XXX.XXX.149
    XXX.XXX.XXX.150
    XXX.XXX.XXX.151
    XXX.XXX.XXX.152
    XXX.XXX.XXX.153
    XXX.XXX.XXX.154
    XXX.XXX.XXX.155
    XXX.XXX.XXX.156
    XXX.XXX.XXX.157
    XXX.XXX.XXX.158
    XXX.XXX.XXX.159
    XXX.XXX.XXX.160
    XXX.XXX.XXX.161
    XXX.XXX.XXX.162
    XXX.XXX.XXX.163
    XXX.XXX.XXX.164
    XXX.XXX.XXX.165
    XXX.XXX.XXX.166
    XXX.XXX.XXX.167
    XXX.XXX.XXX.168
    XXX.XXX.XXX.169
    XXX.XXX.XXX.170
    XXX.XXX.XXX.171
    XXX.XXX.XXX.172
    XXX.XXX.XXX.173
    XXX.XXX.XXX.174
    XXX.XXX.XXX.175
    XXX.XXX.XXX.176
    XXX.XXX.XXX.177
    XXX.XXX.XXX.178
    XXX.XXX.XXX.179
    XXX.XXX.XXX.180
    XXX.XXX.XXX.181
    XXX.XXX.XXX.182
    XXX.XXX.XXX.183
    XXX.XXX.XXX.184
    XXX.XXX.XXX.185
    XXX.XXX.XXX.186
    XXX.XXX.XXX.187
    XXX.XXX.XXX.188
    XXX.XXX.XXX.189
    XXX.XXX.XXX.190
    XXX.XXX.XXX.191
    XXX.XXX.XXX.192
    XXX.XXX.XXX.193
    XXX.XXX.XXX.194
    XXX.XXX.XXX.195
    XXX.XXX.XXX.196
    XXX.XXX.XXX.197
    XXX.XXX.XXX.198
    XXX.XXX.XXX.199
    XXX.XXX.XXX.200
    XXX.XXX.XXX.201
    XXX.XXX.XXX.202
    XXX.XXX.XXX.203
    XXX.XXX.XXX.204
    XXX.XXX.XXX.205
    XXX.XXX.XXX.206
    XXX.XXX.XXX.207
    XXX.XXX.XXX.208
    XXX.XXX.XXX.209
    XXX.XXX.XXX.210
    XXX.XXX.XXX.211
    XXX.XXX.XXX.212
    XXX.XXX.XXX.213
    XXX.XXX.XXX.214
    XXX.XXX.XXX.215
    XXX.XXX.XXX.216
    XXX.XXX.XXX.217
    XXX.XXX.XXX.218
    XXX.XXX.XXX.219
    XXX.XXX.XXX.220
    XXX.XXX.XXX.221
    XXX.XXX.XXX.222
    XXX.XXX.XXX.223
    XXX.XXX.XXX.224
    XXX.XXX.XXX.225
    XXX.XXX.XXX.226
    XXX.XXX.XXX.227
    XXX.XXX.XXX.228
    XXX.XXX.XXX.229
    XXX.XXX.XXX.230
    XXX.XXX.XXX.231
    XXX.XXX.XXX.232
    XXX.XXX.XXX.233
    XXX.XXX.XXX.234
    XXX.XXX.XXX.235
    XXX.XXX.XXX.236
    XXX.XXX.XXX.237
    XXX.XXX.XXX.238
    XXX.XXX.XXX.239
    XXX.XXX.XXX.240
    XXX.XXX.XXX.241
    XXX.XXX.XXX.242
    XXX.XXX.XXX.243
    XXX.XXX.XXX.244
    XXX.XXX.XXX.245
    XXX.XXX.XXX.246
    XXX.XXX.XXX.247
    XXX.XXX.XXX.248
    XXX.XXX.XXX.249
    XXX.XXX.XXX.250
    XXX.XXX.XXX.251
    XXX.XXX.XXX.252
    XXX.XXX.XXX.253
    XXX.XXX.XXX.254
    XXX.XXX.XXX.255
    
    There are 37 addresses in use and 9435 addresses available.
    So, first of all, the outputted number of addresses in use (37) is correct, but the addresses available is waaay to high, it should be 255-37 (=218). Also, as you see, when array_New is being printed, it seems to be a duplicate of array_Sample, it doesn't filter out any of the addresses in use (those in array_Used)

    Any suggestions? It's almost as if, in the Compare_Arrays method, the first condition is being ignored all together, and the else statement is running no matter what, but that can't be because count_Used (number of addresses in use) is being outputted with the correct number.

    Thanks!

  8. #8
    Join Date
    Oct 2005
    Posts
    10

    please help!

    if anybody has any suggestions, please help me out...i've been stuck on this for weeks now, and I cannot figure out why it isnt working, it all looks right to me...

    thanks!

  9. #9
    Join Date
    Oct 2005
    Posts
    10
    i guess it would be useful if i provided the code, sorry!

    Code:
       import java.util.*;
      
        public class Free_IP2 {
        
          public static int count_New = 0;
          public static int count_Used = 0;
       
           public static void main(String[] args) {
          
             int[] array_Used = {1,13,14,21,21,21,22,22,22,22,27,28,30,33,38,39,51,52,62,63,73,81,89,99,101,104,124,154,156,189,199,201,213,229,235,251,253};
          
             int length = 255;     
            
             int[] array_Sample = new int[length+1];
             int[] array_New = new int[length+1];
          	
             Initialize_Array(array_Sample, length);
             Compare_Arrays(array_Used, array_Sample, array_New);
          	
             for (int d = 0; d < array_New.length; d++){
             
                System.out.println("XXX.XXX.XXX." + array_New[d]);
             	
             }
          	
             System.out.println("");
             System.out.println("There are " + Free_IP2.count_Used + " addresses in use and " + Free_IP2.count_New + " addresses available.");
          	
          }
       	
           public static int[] Initialize_Array(int[] array_Sample, int length){
          
             for (int i=0; i<length+1; i++){							
             
                array_Sample[i] = i;  								
             															
             }    
          	
             return array_Sample;
          
          }
       	
           public static int[] Compare_Arrays(int[] array_Used, int[] array_Sample, int[] array_New){
          
             for (int m=0; m<array_Sample.length; m++){
                for(int n=0; n<array_Used.length; n++) {
                
                   if (array_Used[n] == array_Sample[m]){
                      Free_IP2.count_Used++;
                   }
                   
                   else{
                      array_New[m] = array_Sample[m];
                      Free_IP2.count_New++;
                   }
                   
                }
             }
             return array_New;
          } 	
       	
       }

Similar Threads

  1. Replies: 1
    Last Post: 06-12-2006, 11:20 AM
  2. Java Doubly Circular Linked List
    By xsouldeath in forum Java
    Replies: 1
    Last Post: 11-27-2005, 11:19 PM
  3. Trouble buffering transparent gif
    By axlan in forum Java
    Replies: 2
    Last Post: 06-22-2005, 08:21 AM
  4. Replies: 0
    Last Post: 11-19-2001, 12:11 AM
  5. operating system program in java
    By bina in forum Java
    Replies: 2
    Last Post: 09-18-2001, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center