IpAdressFormatCheck
/** ######################################################################## # クラス名 : IpAdressFormatCheck # 概要 : IPアドレス形式かチェックを実施する # 引数 : String ip (IPアドレス) # 戻り値 : boolean bl (true:IPアドレス形式 falss:IPアドレス形式ではない) ############################################################################# **/ public class IpAdressFormatCheck { // String型を受取 public static boolean IpAdressFormatCheck(String ip) { boolean bl; // IPアドレスを.で分割 String[] arrayIp = ip.split("\\."); // 長さが4以外の場合エラー if (arrayIp.length != 4) { bl = false; return bl; } // 数字に変換出来ない場合エラー for( String ips : arrayIp ) { try { Integer.parseInt(ips); }catch(NumberFormatException e) { bl = false; return bl; } } // 数字が0~255以外の場合エラー for( String ips : arrayIp ) { if(Integer.parseInt(ips) < 0 || Integer.parseInt(ips) > 255) { bl = false; return bl; } } bl = true; return bl; } }