import java.util.Scanner;
public class Main {
static String name;//存放常量名
static String value;//存放常量值
static String type;//常量类型
static String err;//错误信息存放
static int corrName;//0表示常量名错误
static int corrValue;//0表示常量值错误
static int int_num=0;
static int string_num=0;
static int float_num=0;
static int char_num=0;//统计各种类型的常量数量
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("please input a string:");
String s=in.nextLine();
s=s.trim();//去除首尾空格
boolean result =s.startsWith("const")&&s.endsWith(";");
while (!result){
//当输入的字符串不是以const开头分号结尾,则输出错误信息,并重新输入
System.out.println("It is not a constant declaration statement!");
System.out.println("Please input a string again!");
s=in.nextLine();
s=s.trim();
result =s.startsWith("const")&&s.endsWith(";");
}
Output(s);
in.close();
}
//判断常量名是否合法
public static int checkName(char[]a,int i){
name="";
while (a[i]!='='){
name+=a[i];
i++;
}
name=name.trim();
String regex="[a-zA-Z_][a-zA-Z0-9_]*";
boolean result=name.matches(regex);
if (result) {
corrName = 1;
} else{
corrName=0;
}
return i;
}
//判断常量值的合法性与常量类型
public static int checkType(char a[],int i){
value="";
err="";
while (true){
value+=a[i];
i++;
char b[]=value.toCharArray();
if(b[0]!='"'&&(a[i]==','||a[i]==';')){
break;
}
else if(b[0]=='"'&&a[i]=='"'&&(a[i+1]==','||a[i+1]==';')){
value+=a[i];
break;
}
else
continue;
}
System.out.println("value="+value);
value = value.trim();
if (corrName==1){
//判断是否为整数
if(value.matches("[+|-]?[0-9]*")){
String s1=value;
//判断符号
if(value.charAt(0)=='+'||value.charAt(0)=='-'){
s1=value.substring(1);
}
if (s1.equals("0")||s1.matches("[1-9][0-9]*")){
corrValue=1;
type="integer";
int_num++;
}else{
err="Wrong!The integer cannot be started whit'0";
corrValue=0;
}
}
//判断该数是否为浮点数
else if(value.matches("[+|-]?[0-9]*[.][0-9]*")||value.matches("[+|-]?[0-9]*[.][0-9]+e[+|-]?[0-9]*")){
corrValue=1;
type="float";
float_num++;
}
//判断是否是char型
else if(value.startsWith("'")&&value.endsWith("'")){
if(value.length()==3){
corrValue=1;
type="char";
char_num++;
}
else{
err+="Wrong!There are more than char in .";
corrValue=0;
}
}
//判断常量是String
else if(value.startsWith("\"")&&value.endsWith("\"")){
corrValue=1;
type="String";
string_num++;
}
//其他错误
else {
corrValue=0;
err+="Wrong!The format of the value string is not correct!";
}
}
return i;
}
static void Output(String s){
char str[]=s.toCharArray();
int i=5;
while (i<str.length-1){
i=checkName(str,i);
i=checkType(str,i+1)+1;
if (corrName==1){
//常量值正确,输出结果
if(corrValue==1){
System.out.println(name+"("+type+","+value+")");
}
else {
System.out.println(name+"("+err+")");
}
}
else {
System.out.println(name+"("+"Wrong!It is not a identifier! "+")");
}
}
System.out.println("int_num="+int_num+";char_num="+char_num+";string_num"+string_num+";float_num="+float_num+".");
}
}
测试结果:
补充:
import java.util.Scanner;
public class Main {
static String name;//存放常量名
static String value;//存放常量值
static String type;//常量类型
static String err;//错误信息存放
static int corrName;//0表示常量名错误
static int corrValue;//0表示常量值错误
static int int_num=0;
static int string_num=0;
static int float_num=0;
static int char_num=0;//统计各种类型的常量数量
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("please input a string:");
String s=in.nextLine();
s=s.trim();//去除首尾空格
boolean result =s.startsWith("const")&&s.endsWith(";");
while (!result){
//当输入的字符串不是以const开头分号结尾,则输出错误信息,并重新输入
System.out.println("It is not a constant declaration statement!");
System.out.println("Please input a string again!");
s=in.nextLine();
s=s.trim();
result =s.startsWith("const")&&s.endsWith(";");
}
Output(s);
in.close();
}
//判断常量名是否合法
public static int checkName(char[]a,int i){
name="";
while (a[i]!='='){
name+=a[i];
i++;
}
name=name.trim();
String regex="[a-zA-Z_][a-zA-Z0-9_]*";
boolean result=name.matches(regex);
if (result) {
corrName = 1;
} else{
corrName=0;
}
return i;
}
//判断常量值的合法性与常量类型
public static int checkType(char a[],int i){
value="";
err="";
while (true){
value+=a[i];
i++;
char b[]=value.toCharArray();
if(b[0]!='"'&&b[0]!='\''&&(a[i]==','||a[i]==';')){
break;
}
else if(b[0]=='"'&&a[i]=='"'&&(a[i+1]==','||a[i+1]==';')){
value+=a[i];i++;
break;
}
else if(b[0]=='\''&&a[i]=='\''&&(a[i+1]==','||a[i+1]==';')){
value+=a[i];i++;
break;
}
else
continue;
}
System.out.println("value="+value);
value = value.trim();
if (corrName==1){
//判断是否为整数
if(value.matches("[+|-]?[0-9]*")){
String s1=value;
//判断符号
if(value.charAt(0)=='+'||value.charAt(0)=='-'){
s1=value.substring(1);
}
if (s1.equals("0")||s1.matches("[1-9][0-9]*")){
corrValue=1;
type="integer";
int_num++;
}else{
err="Wrong!The integer cannot be started whit'0";
corrValue=0;
}
}
//判断该数是否为浮点数
else if(value.matches("[+|-]?[0-9]*[.][0-9]*")||value.matches("[+|-]?[0-9]*[.][0-9]+e[+|-]?[0-9]*")){
corrValue=1;
type="float";
float_num++;
}
//判断是否是char型
else if(value.startsWith("'")&&value.endsWith("'")){
if(value.length()==3){
corrValue=1;
type="char";
char_num++;
}
else{
err+="Wrong!There are more than char in .";
corrValue=0;
}
}
//判断常量是String
else if(value.startsWith("\"")&&value.endsWith("\"")){
corrValue=1;
type="String";
string_num++;
}
//其他错误
else {
corrValue=0;
err+="Wrong!The format of the value string is not correct!";
}
}
return i;
}
static void Output(String s){
char str[]=s.toCharArray();
int i=5;
while (i<str.length-1){
i=checkName(str,i);
i=checkType(str,i+1)+1;
if (corrName==1){
//常量值正确,输出结果
if(corrValue==1){
System.out.println(name+"("+type+","+value+")");
}
else {
System.out.println(name+"("+err+")");
}
}
else {
System.out.println(name+"("+"Wrong!It is not a identifier! "+")");
}
}
System.out.println("int_num="+int_num+";char_num="+char_num+";string_num"+string_num+";float_num="+float_num+".");
}
}