Java正则表达式校验十三位纯数字字符串

java

信息论与编码课后作业

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class EAAN13 {

public static void main(String[] args) {

isEAN13();

}

private static void isEAN13(){

int cho;

Scanner scanner = new Scanner(System.in);

Scanner scanner1 = new Scanner(System.in);

String demo= "";

while (true){

System.out.println("*****************");

System.out.println("*****1、校验EAN13******");

System.out.println("*****2、退出程序*******");

System.out.println("*****************");

System.out.print("请选择:");

cho = scanner.nextInt();

switch (cho) {

case 1:

System.out.println("请输入你要转换的EAN13:");

demo = scanner1.next();

int end = isNumeric(demo);

if (end == 1){

System.out.println("校验码合格!");

}else{

System.out.println("您的输入不符合EAN13规定的要求请重新选择:");

isEAN13();

}

break;

case 2:

System.exit(0);

break;

default:

System.out.println("输入错误!");

}

}

}

private static int isNumeric(String str)

//正则表达式校验13位数字

{

Pattern pattern = Pattern.compile("\\d{13}");

//System.out.println(pattern.pattern());

Matcher isNum = pattern.matcher(str);

if( !isNum.matches() )

{

return 0;

}else{

return 1;

}

}

}

以上是 Java正则表达式校验十三位纯数字字符串 的全部内容, 来源链接: utcz.com/z/392396.html

回到顶部