我的代码中缺少了什么?JAVA

我的代码中缺少了什么?JAVA,java,string,Java,String,这是我的代码块。我使用for循环遍历字符串,从一个由空格分隔的字中取出一个标记,确定它是否是保留字,如果是,它将打印保留字is:,否则它将打印当前字is:。这就是我到目前为止得到的,我一直在研究如何使isKeyWord工作。 注意:我不能使用分割函数、扫描程序或标记器。只是一个循环 public class Week3Project { final static String program = "/*\n" + " * To change this license header, ch

这是我的代码块。我使用for循环遍历字符串,从一个由空格分隔的字中取出一个标记,确定它是否是保留字,如果是,它将打印保留字is:,否则它将打印当前字is:。这就是我到目前为止得到的,我一直在研究如何使isKeyWord工作。 注意:我不能使用分割函数、扫描程序或标记器。只是一个循环

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.


            }
        }

您的循环版本在检查之前会附加空格/制表符/换行符,这会导致当前单词与您的关键字永远不匹配。

检查这是否有帮助

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] 保留字为:[双精度] 保留字为:[导入] 保留字为:[int] 保留字为:[新] 保留字为:[包] 保留字为:[公共] 保留字为:[静态] 保留字是:[这个]
保留字是:[void]

首先,我可能会将包含关键字的字符串[]移动到一个静态的最终字段,但我没有将它粘贴到这里,因为它很长,并且不需要更改。接下来,与所有这些连接相比,更喜欢StringBuilder。而Character.isWhitespacechar是一种很好的确定是否遇到空白的方法。差不多

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);
    }
}

拆下断路器。不要在循环中声明isKeyWord。修正你的缩进。谢谢。我尝试了很多方法来解决这个问题,我花了大约10个小时。。。哎哟,我想是休息;在错误的地方。它应该紧跟在isKeyWord=true之后;,因此,如果我们找到匹配的关键字,我们就不会检查每个关键字。我想你是对的,但是我仍然无法让它打印保留的单词。它直接转到打印当前单词的else语句。hmmyou正在无条件地将str.charAti添加到当前单词字符串中。然后检查str.charAti是否=“”…,这意味着s包含一个空格或制表符,或者换行字符太多谢谢谢谢!!!!让我们成为最好的朋友:,如果输入字符串包含classed、Doot、doubles或imported等词,会发生什么情况。。。。哪些不是Java关键字?例如,doubles和double都包含do。
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);
    }
}
boolean isKeyWord = Arrays.stream(keywords).anyMatch(token::equals);