这是我目前的计划。它适用于给定的字符串INPUT。 但不适合其他投入。我希望它能为其他人工作,如果我< 30然后它也应该从中检测文字,关键字和变量。
请指导我错误并帮我纠正。 提前谢谢。
这是完整的课程。
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JLabel;
public class Test {
static String[] keyword = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "continue",
"default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "if",
"implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private",
"protected", "public", "return", "short", "static", "string", "strictfp", "super", "switch", "this",
"throw", "throws", "transient", "try", "void", "volatile", "while"};
static boolean labelsrun = false;
private static String REGEX = "((?=[a-z_0-9]|[a-z])[a-z_0-9]+(?=\\s*=))";
private static String INPUT = "public static void main(String[]args)\n" + "{\n" + " String a=\"Malik\";\n"
+ " int i=1;\n" + " double j=2;\n" + "}";
static JLabel identifiers = new JLabel(""), literals = new JLabel(""), keywords = new JLabel("");
public static void check_Identifiers(String a[]) {
int count = 0;
for (int i = 2; i < a.length; i++) {
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(a[i]);
if (m.find()) {
if (count == 0) {
identifiers.setText(identifiers.getText() + " " + m.group());
count++;
} else if (count > 0) {
identifiers.setText(identifiers.getText() + ", " + m.group());
}
}
}
count = 0;
}
public static void check_Literals(String a[]) {
int count = 0;
String regex = "=.*";
String b = "";
for (int i = 2; i < a.length; i++) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(a[i]);
if (m.find()) {
if (m.group().contains("=")) {
b = m.group();
b = b.replaceAll("=", "");
b = b.replace(";", "");
}
if (count == 0) {
literals.setText(literals.getText() + " " + b);
count++;
} else if (count > 0) {
literals.setText(literals.getText() + ", " + b);
}
}
}
count = 0;
}
public static void check_Keywords(String a[]) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < keyword.length; j++) {
String regex = ".*\\b" + keyword[j] + "\\b.*";
if (a[i].matches(regex)) {
if (count == 0) {
keywords.setText(keywords.getText() + " " + keyword[j]);
count++;
} else if (count > 0) {
keywords.setText(keywords.getText() + ", " + keyword[j]);
}
}
}
}
count = 0;
}
public static void main(String[] args) {
String a[] = INPUT.toLowerCase().split("\n");
check_Identifiers(a);
check_Literals(a);
check_Keywords(a);
}
}
这是有问题的方法。目前,仅当输入具有=运算符时才有效。但是,它如何使它适用于所有运营商?或者至少> &LT;与=
一起public void check_Literals(String a[]) {
int count = 0;
String regex = "=.*";
String b = "";
for (int i = 2; i < a.length; i++) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(a[i]);
if (m.find()) {
if (m.group().contains("=")) {
b = m.group();
b = b.replaceAll("=", "");
b = b.replace(";", "");
}
if (count == 0) {
literals.setText(literals.getText() + " " + b);
count++;
} else if (count > 0) {
literals.setText(literals.getText() + ", " + b);
}
}
}
count = 0;
}