开发者

another homework question error unexpected type required:variable found:value

I have another error, unexpected type required:variable found:value and I can't figure out why. The code is as follows:

public class ISBNText extends JTextField
{  
    protected static final int ISBN_NUM=10;
    protected static String bookNum;
    protected JTextField  bookText; 
    protected String valid;
    public ISBNText() 
    {
        super(20);
    }   

    public String getISBN()
    {           
        bookNum = getText();
        return bookNum;
    }

    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(bookNum)=false)      //////////COMPILER   ERROR    HERE////////
            throw new ISBNException ("ISBN " + bookNum + " is invalid.\n" + 
                "ISBN " + bookNum + " should be " + validnum);
        if (check=true) return bookNum;
    }                             

    public boolean checkDigit (String bookNum)
    {
        boolean status;
        double total=0.0;
        char[] check   = {0,1,2,3,4,5,6,7,8,9,X};
        int[] checkNums= {0,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];
        }    
        if ((checkNums[9] % 11)==check[9])
            status = true;
        else 
            status=false;
    }
}

This program is being used to check the validity of an ISBN number entered by a user. I don't understand why it says it finds value and it expects a variable, as bookNum is a variable. I have no doubt that this is due to my limited knowledge. Hoping you guys can help me out one more time.


This assignment (which doesn't make sense)

if (checkDigit(bookNum)=false)

should rather have been an equation

if (checkDigit(bookNum)==false)

or, better, just an expression

if (!checkDigit(bookNum))

See also:

  • Java tutorial - equality operators
  • Java tutorial - if statements


checkDigit(bookNum)=false should be !checkDigit(bookNum)

Same thing for if (check=false) which should be if (!check) Another with if (check=true)

Also,

for (int i=0;i<bookNum.length;i++)
{
    check(i)=bookNum[i];
    total+=check[i]*checkNums[i];
}    

Should be :

for (int i=0;i<bookNum.length();i++)
{
    (something, but definitely not a function result)=bookNum.charAt(i);
    total+=check[i]*checkNums[i];
}

Plus,

char[] check   = {0,1,2,3,4,5,6,7,8,9,X}; // X won't compile

Plus,

checkDigit has no return.

Plus,

validnum is not declared


You need to use charAt, not the array index notation on the bookNum variable in the checkDigit method. Strings are not arrays.


You need to use ==, not =. The former checks for equality, the latter is an assignment operator. Alternatively, you could just do if (!checkDigit(bookNum)), rather than an explicit comparison to false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜