码迷,mamicode.com
首页 > 其他好文 > 详细

ip地址合法性

时间:2015-08-17 21:07:04      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 

/*
* Java语法上正则化表达式的使用技巧,对于‘.‘要用‘\\.‘

(2)注意空字符串“”和null的区别,判断一个字符串是不是空字符串用.equals("")
* (1,判断是否有三个‘.’;2,判断三个点之间是否不相邻;
* 3,判断每个部分是否是数字;4,判断每个数字是否在0到255之间。)
*/
import java.util.Scanner;

public class IPMatch {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
String[] num = string.split("\\.");
boolean result = true;
if (num.length != 4) {
result = false;
}
for (int i = 0; i < num.length; i++) {
if (num[i].equals("")) {
System.out.println("NO"); // 如果为空字符串就返回
break;//注意break不能丢。否则会继续执行。
}
}
for (String string2 : num) {
int no = Integer.parseInt(string2);
if (no >= 0 && no <= 255) {
} else {
result = false;
break;
}
}
if (result) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}

ip地址合法性

标签:

原文地址:http://www.cnblogs.com/wushuai-study/p/4737390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!