jAVA 识别有效的IP地址和掩码并进行分类统计





import java.util.*;


public class Main{
static int a = 0;
     static int b = 0;
     static int c = 0;
     static int d = 0;
     static int e = 0;
     static int err = 0;
     static int priIp = 0;
public static void main(String args[]) {


    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine()) {
    String s = sc.nextLine();
    countABCDEEP(s);
   
    }
    System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+err+" "+priIp);
}

public static void countABCDEEP(String s) {
String[] str = s.split("~");
String[] ipk = str[0].split("\\.");
String[] mask = str[1].split("\\.");
if(!isValidIP(ipk)) {
err++;
return;
}


if(!isValidMark(mask)) {
err++;
return;
}

if(isPriIp(ipk)) {
priIp++;
}

isABCDEIp(ipk);
}

public static boolean isValidIP(String ipk[]) {
if(ipk.length!=4)
return false;
for(int i=0;i<ipk.length;i++) {
int ipInt = Integer.parseInt(ipk[i]);
if(ipk[i].equals("")||ipk[i].equals(" ")||ipInt<0||ipInt>255) {
return false;
}
}
return true;
}

public static boolean isValidMark(String mark[]) {
if(mark.length!=4)
return false;
int markInt[] = new int[mark.length];
for(int i=0;i<mark.length;i++) {
markInt[i] = Integer.parseInt(mark[i]);
}
for(int i=0;i>mark.length;i++) {
if(mark[i].equals("")||mark[i].equals(" ")||markInt[i]<0||markInt[i]>255) {
return false;
}
}

if(markInt[0]+markInt[1]+markInt[2]+markInt[3]==255*4)
return false;

int mask=0;
    for(int i=0;i<4;i++){
         mask+= markInt[i]<<((3-i)*8);
    }
     
    mask=~mask+1;
    mask=mask&(mask-1);
    if(mask!=0)
    return false;

return true;
}

public static boolean isPriIp(String ipk[]) {
int ip0 = Integer.parseInt(ipk[0]);
int ip1 = Integer.parseInt(ipk[1]);
if ((ip0 == 10)
                 || (ip0 == 172 && ip1 >= 16 && ip1 <= 31)
                 || (ip0 == 192 && ip1 == 168)) 


return true;
return false;
}

public static void isABCDEIp(String[] ipk) {
int[] ipInt = new int[ipk.length];
for(int i=0;i<ipk.length;i++) {
ipInt[i] = Integer.parseInt(ipk[i]);
}
if(ipInt[0]>=1&&ipInt[0]<=126){
            a = a+1;
        }
        if(ipInt[0]>=128&&ipInt[0]<=191){
            b = b+1;
        }
        if(ipInt[0]>=192&&ipInt[0]<=223){
            c = c+1;
        }
        if(ipInt[0]>=224&&ipInt[0]<=239){
            d = d+1;
        }
        if(ipInt[0]>=240&&ipInt[0]<=255){
            e = e+1;
        }


        return;


}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值