Guest User

Untitled

a guest
Jun 23rd, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package ip;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * @author Zigi
  7.  */
  8. public class IP {
  9.  
  10.     private static final int IP_PARTS = 4;
  11.     private static final int START = 0;
  12.     private static final int STOP = 1;
  13.     private static final int INPUT_SIZE = 2;
  14.     private static final int PART = 256;
  15.     private static final int PART2 = 256 * 256;
  16.     private static final int PART3 = 256 * 256 * 256;
  17.    
  18.     private static boolean isValidIP(final String[] parts)  {
  19.         if(parts.length != IP_PARTS) {
  20.             System.out.print("Invalid IP structure! ");
  21.             return false;
  22.         }
  23.        
  24.         for(int i = 0; i < IP_PARTS; i++){
  25.             int partNum = -1;
  26.             try {
  27.                 partNum  = Integer.parseInt(parts[i]);
  28.             } catch (NumberFormatException e) {
  29.                 System.out.print("Non-number entered at position " + (i + 1) + "! ");
  30.                 return false;
  31.             }
  32.             if (partNum < 0 || partNum >= PART) {
  33.                 System.out.print(
  34.                     "The number at position " + (i + 1) +
  35.                     " should be in the range [0; 255]! "
  36.                 );
  37.                 return false;
  38.             }
  39.         }
  40.            
  41.         return true;
  42.     }
  43.    
  44.     private static long getNumber(final String[] parts) {
  45.        long number = 0L, exp = 24L;
  46.        
  47.        for(int position = 0; position < IP_PARTS; position++){
  48.            number += (long)Integer.parseInt(parts[position]) << exp;
  49.            exp -= 8L;
  50.        }
  51.        return number;
  52.     }
  53.    
  54.     public static void main(String[] args) {
  55.         Scanner input = new Scanner(System.in);
  56.         String parts[][] = new String[INPUT_SIZE][];
  57.         String types[] = {"first", "last"};
  58.        
  59.         for(int i = 0; i < INPUT_SIZE; i++)
  60.             do {
  61.                 System.out.print("Please input " + types[i] + " ip address: ");
  62.                 String ip = input.nextLine();
  63.                 parts[i] = ip.split("\\.");
  64.             } while (!isValidIP(parts[i]));
  65.        
  66.         long start = getNumber(parts[START]), stop = getNumber(parts[STOP]) + 1;
  67.         for(long ip = start; ip < stop; ip++) {
  68.             System.out.println(
  69.                 ip / PART3 % PART + "." +
  70.                 ip / PART2 % PART + "."  +
  71.                 ip / PART % PART + "."  +
  72.                 ip % PART
  73.             );
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment