这是我的代码块。我正在使用for循环遍历字符串,拉出令牌(一个用空格分隔的单词),确定它是否是保留字,如果这样,它将显示“ Reserved word is:”,否则将显示“当前单词是:“。到目前为止,这是我所得到的,我仍然坚持如何使isKeyWord工作。 注意:我不能使用分割功能,扫描器或令牌生成器。只是一个for循环。
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * @author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * @param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
public static void main(String[] args)
{
String str = program;
String s = "";
String[] keywords = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum",
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n') {
String currentWord = s.toString();
//System.out.println(currentWord);
boolean isKeyWord = false;
for (int j = 0; j < keywords.length; j++) {
if (currentWord.equalsIgnoreCase(keywords[j])) {
isKeyWord = true;
}
} break; } }
if (isKeyWord == true) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]")
}
s = "";//Clear the string to get it ready to build next token.
}
}
答案 0 :(得分:2)
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n') {
// end of token, check if key word
String currentWord = s.toString();
boolean isKeyword = false
for (int j = 0; j < keywords.length; j++) {
if (currentWord.equalsIgnoreCase(keywords[j])) {
isKeyword = true;
break;
}
}
if(isKeyword) {
System.out.println("Reserved word is: [" + currentWord + "]");
} else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
} else {
// continue building token
s += str.charAt(i) + "";
}
}
您的循环版本会在检查之前添加空格/制表符/换行符,这会导致当前单词与您的关键字永远不匹配。
答案 1 :(得分:0)
首先,我可能会将包含String[]
的{{1}}移到keywords
字段中(但是我没有在这里粘贴它,因为它很长-而且不会< strong> 需要 进行更改)。接下来,在所有这些串联中首选static final
。 StringBuilder
是确定您是否遇到空白的好方法。像
Character.isWhitespace(char)
而且,在Java 8+中,数组搜索可能可以通过StringBuilder sb = new StringBuilder();
for (int i = 0; i < program.length(); i++) {
char ch = program.charAt(i);
if (!Character.isWhitespace(ch)) {
sb.append(ch);
} else {
String token = sb.toString();
if (token.isEmpty()) {
continue;
}
boolean isKeyWord = false;
for (String word : keywords) {
if (word.equals(token)) {
isKeyWord = true;
break;
}
}
System.out.printf("%s word is : [%s]%n",
isKeyWord ? "Reserved" : "Current", token);
sb.setLength(0);
}
}
和Arrays.stream
来完成
anyMatch
答案 2 :(得分:-1)
检查是否可以帮助您
public static void main(String[] args) {
String[] keywords = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum",
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private",
"protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
for (String value : keywords) {
if (program.contains(value)) {
System.out.println("Reserved word is: [" + value + "]");
}
}
}
我的结果
保留字是:[class]
保留字是:[do]
保留字是:[double]
保留字是:[import]
保留字是:[int]
保留字为:[新]
保留字是:[package]
保留字是:[public]
保留字是:[静态]
保留字是:[this]
保留字是:[void]