简体   繁体   中英

method declaration gives an error. What gives?

So I'm still plugging away at my big assignment (thanks to everyone for helping me figure out the problem with my loop). Now i have moved on to start making a checkDigit() method that is used to validate whether the check digit on the isbn # is valid (which I have only started screwing around with). Here's the problem: I tried to compile it, but the compiler tells me that there is an expected at the method call. Here is a snippet of my beginner's code:

    private String validateISBN(String bookNum)throws ISBNException
{
    boolean check=false;
    bookNum.replaceAll("-","");
    if (bookNum.length()!=ISBN_NUM)
        throw new ISBNException ("ISBN "+ bookNum + " must be 10 characters");
    for (int i=0;i<bookNum.length()-1;i++)
    {
        if (Character.isDigit(bookNum.charAt(i)))
            check=true;
    }  
    if (bookNum.charAt(9)=='X') check=true;
    if (Character.isDigit(bookNum.charAt(9))) check=true;
    if (check=false) throw new ISBNException ("ISBN " + bookNum + " must contain all digits" + 
                "or 'X' in the last position");
    if (checkDigit()=false)
            throw new ISBNException ("ISBN " + bookNum + " is invalid.\n" + 
                "ISBN " + bookNum + " should be " + validnum);
    if (check=true) return bookNum;
}                             
public boolean checkDigit (bookNum)
{
    double total=0.0;
    char[] check   = {1,2,3,4,5,6,7,8,9,X};
    int[] checkNums= {1,2,3,4,5,6,7,8,9,10};
    for (int i=0;i<bookNum.length;i++)
        check(i)=bookNum[i];
        total+=check[i]*checkNums[i];  

What exactly does this mean? Normally it only means that I am missing a curly brace somewhere, but that is not the case this time.

edit

Sorry for the obvious and stupid question. For you guys who asked to see the actual compiler message, I put it up top (buried), it was that the compiler

identifier expected

but I actually put the <> tags around identifier, so it took out the word identifier, and now it says, "....but the compiler tells me that there is an expected at the method call, instead of, " ....but the compiler tells me that there is an identifier expected at the method call.

And the enter that was originally at the start was from the text field (which I am typing the question) for when I pasted the code, it said 'enter code here', and I didn't take out the enter.

Sorry

Well it appears you are in fact missing the closing curly brace for your checkDigit method, plus what is that stray enter doing before the validateISBN method? Also, you need to type the bookNum parameter to your checkDigit method, ie, checkDigit(String bookNum)

And lastly, you're attempting to call checkDigit() without passing in any arguments here:

if (checkDigit()=false)
            throw new ISBNException ("ISBN " + bookNum + " is invalid.\n" + 
                "ISBN " + bookNum + " should be " + validnum);

You need to provide bookNum in this method call.

Furthermore, it is an excellent idea to post the compiler message along with your question; someone's almost immediately going to ask you for it, so just do it. Plus, if you take a minute to read the compiler message, it's usually pretty obvious what the problem, at least for something as simple as incorrect syntax.

乍一看,您似乎在validateISBN的方法签名之前enter了一个杂项,并且还缺少checkDigit方法的bookNum参数的类型声明。

如果将“ X”放在检查数组的单引号中,是否可以解决问题?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM