欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)IP地址到二進(jìn)制的轉(zhuǎn)換

 更新時間:2021年11月26日 08:46:38   作者:e夢.  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)IP地址到二進(jìn)制的轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Java編程實(shí)現(xiàn)十進(jìn)制IP地址到二進(jìn)制IP地址的轉(zhuǎn)換。

如:192.168.1.100,轉(zhuǎn)換后:11000000.10101000.00000001.01100100

要求:

1.定義自定義異常類InvalidIP(檢查型的),代表IP地址非法---如:點(diǎn)分十進(jìn)制IP段數(shù)不是4;各段數(shù)值不是0~255范圍

2.定義公有靜態(tài)方法convertIP實(shí)現(xiàn)轉(zhuǎn)換,點(diǎn)分十進(jìn)制IP地址為參數(shù),轉(zhuǎn)換后的二進(jìn)制IP為返回值,在方法內(nèi)檢查參數(shù)IP地址的合法性,如非法,請拋出自定義異常InvalidIP

public static String convertIP(String ip)throws InvalidIP

提示:

(1)先做字符串拆分,"."為分隔符,存入String[];

(2)再做字符串到int的轉(zhuǎn)換,考慮進(jìn)制問題,生成int[];

(3)對數(shù)組長度做判斷,不是4,則IP非法拋出異常;對轉(zhuǎn)換后的數(shù)值做判斷,不是0~255范圍,則IP非法拋出異常。

3.在主方法中實(shí)現(xiàn)用戶輸入、方法調(diào)用及輸出,即:

(1)通過Scanner類的nextLine()讀取鍵盤輸入,或者通過JOptionPane.showInputDialog(String str)實(shí)現(xiàn)對話框文本輸入,得到欲轉(zhuǎn)換的點(diǎn)分十進(jìn)制IP字符串str;

(2)調(diào)用convertIP(str)實(shí)現(xiàn)轉(zhuǎn)換并得到返回值,此處需有異常處理;

(3)輸出結(jié)果,命令行方式,或者對話框方式JOptionPane.showMessageDialog(String str)

import com.sun.deploy.util.StringUtils;
import java.util.Arrays;
import java.util.Scanner;
 
class InvalidIP extends Exception {  //定義自定義異常類InvalidIP(檢查型的),代表IP地址非法---如:點(diǎn)分十進(jìn)制IP段數(shù)不是4;各段數(shù)值不是0~255范圍
        InvalidIP(String s) {
            super(s);
            return;
        }
    }
 
public class Test6_1 {
    public static String convertIP(String str) throws InvalidIP { //定義公有靜態(tài)方法convertIP實(shí)現(xiàn)轉(zhuǎn)換,點(diǎn)分十進(jìn)制IP地址為參數(shù),轉(zhuǎn)換后的二進(jìn)制IP為返回值,在方法內(nèi)檢查參數(shù)IP地址的合法性,如非法,請拋出自定義異常InvalidIP
        String[] ipstr = str.split("\\.");
        String return_ip = null;
        if (ipstr.length==4) {
            int[] array = Arrays.stream(ipstr).mapToInt(Integer::parseInt).toArray();
            String[] ipa = new String[4];
            for(int i=0;i<array.length;i++)
            {
                if (array[i]<1 || array[i]>255){
                    throw new InvalidIP("各段數(shù)值不是0~255范圍!"+"\n"); // 出現(xiàn)字段不在1-255之間,拋出異常
                }else{
                    String x = Integer.toBinaryString(array[i]);
                    String ip8 = "00000000";
                    ipa[i] = ip8.substring(0, ip8.length() - x.length()) + x;
                    if(ipa[1]!=null&&ipa[2]!=null&&ipa[3]!=null){
                        String ip = StringUtils.join(Arrays.asList(ipa), ".");
                        return_ip = ip;
                    }
                }
            }
        }else{
            throw new InvalidIP("點(diǎn)分十進(jìn)制IP段數(shù)不是4!"+"\n"); // 字段數(shù)量溢出,拋出異常1
        }
        return return_ip;
    }
    public static void main(String[] args){
        while(true) {
            Scanner in = new Scanner(System.in);
            System.out.println("請輸入IP地址:");
            String str = in.nextLine();
            try {
                System.out.println("轉(zhuǎn)換為二進(jìn)制:"+convertIP(str)+"\n");
            }
            catch(InvalidIP e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論