java Java中的“<标识符>预期”编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5527952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:33:18  来源:igfitidea点击:

"<identifier> expected" compilation error in Java

javaidentifier

提问by MassStrike

I get "<identifier> expected" error on this line....

<identifier> expected在这条线上收到“ ”错误....

  private static String getReducedISBN(char 'x') {

...of this code....

...这个代码....

public class CheckISBN7 {

//private static String originalISBN;  // class variable

public static void main(String[] args) {
    // Prompt the user to enter an ISBN
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();

    // Get the ISBN number without the dashes
    String reducedISBN = getReducedISBN('-');

    // Get the computed check digit
    int computedCheckDigit = getCheckDigit(reducedISBN);

    // Display check digit entered by the user
    System.out.println("Check digit entered: " + originalISBN.charAt(12));

    // Display computed check digit
    System.out.println("Check digit computed: " + computedCheckDigit);
}

private static String getReducedISBN(char 'x') {
    SimpleIO.prompt("Enter ISBN: ");
    String originalISBN = SimpleIO.readLine();
    int dashPos1 = originalISBN.indexOf("x");
    int dashPos2 = originalISBN.indexOf("x", dashPos1 + 1);
    String reducedISBN = originalISBN.substring(0, dashPos1) +
            originalISBN.substring(dashPos1 + 1, dashPos2) +
            originalISBN.substring(dashPos2 + 1, 11);
    return reducedISBN;
}

private static int getCheckDigit(String reducedISBNParameter) {
    int total = 0;
    final String digits = "0123456789X";
    for(int i = 0, j = 10; i <= 8; i++, j++) {
        total += j *
                (Integer.parseInt(reducedISBNParameter.substring(i, i + 1)));
    }
    int checkDigit = 10 - ((total - 1) % 11);
    int computedCheckDigit = digits.charAt(checkDigit);
    return computedCheckDigit;
    }
}

Can't really figure out the problem , any help would be much appreciated.

无法真正弄清楚问题,任何帮助将不胜感激。

回答by BoltClock

You're trying to pass the char value 'x'in your method signature, which isn't valid syntax:

您正在尝试'x'在方法签名中传递 char 值,这是无效的语法:

private static String getReducedISBN(char 'x') {

Did you mean to use xas a variable name?

您的意思是x用作变量名吗?

private static String getReducedISBN(char x) {

As well as here, since I assume you're trying to find the index of whatever you pass as the separator character instead of the string "x":

以及这里,因为我假设您正在尝试查找作为分隔符而不是字符串传递的任何内容的索引"x"

int dashPos1 = originalISBN.indexOf(x);
int dashPos2 = originalISBN.indexOf(x, dashPos1 + 1);

回答by user268396

'x'is not an indentifier (variable or whatever), it is a literal character. Likewise "x" is a literal string. Replace char 'x'with Character xand "x"with x.toString()to get what you want.

'x'不是标识符(变量或其他),它是一个文字字符。同样,“x”是一个文字字符串。替换char 'x'withCharacter x"x"withx.toString()以获得你想要的。