Java Issue

Soldato
Joined
5 Jul 2005
Posts
17,995
Location
Brighton
Hey, need a bit of help with this error I'm getting.

Code:
  public void study()
    {
        if (TextBook book == null){
           book = bookShelf[ nextBook ]; 
           nextBook++;
           card.swipe();
        } else if (isFinished == false) {
            chaptersRead++;
        } else {
            bookShelf[ nextBook ] = null;
        }

I'm a total beginner and really don't know where to start, when I compile it I get the error ')' expected but can't see any brackets that haven't been closed.

I'm not looking for you to write it for me, maybe just point me in the right direction if possible, thanks for looking :)
 
Last edited:
What line?

Hard to say with only a small snippet.. is "isFinished" a variable, or a method? You're also assigning false to isFinished, not comparing.

Also:
Code:
if (TextBook book == null) {
Doesn't look valid. If it's a local variable, it'll be null anyway, if it's an instance variable, then the "TextBook" is superfluous (and probably erroneous!)
 
'book' is a variable in the class Textbook and isFinished is a boolean variable.

It happens at the line;
Code:
if (TextBook book == null){

When you call the method it's supposed to assign a TextBook to the class Student if it doesn't already have one, 'read' a chapter if it does and replace the book in the array bookshelf if it has been finished.

Not sure if it's relevent but I'm using BlueJ.

Again, apologies for being utterly useless at this programming lark :p
 
If book is a variable of the class Textbook shouldn't it be called as:

Code:
if (TextBook[COLOR="Lime"].[/COLOR]book == null)

I've never really used Java but that's the typical way to call a variable from a class in c# and i know they are similar languages.
 
If book is a variable of the class Textbook shouldn't it be called as:

Code:
if (TextBook[COLOR="Lime"].[/COLOR]book == null)

I've never really used Java but that's the typical way to call a variable from a class in c# and i know they are similar languages.

Same with Java.
 
Last edited:
I'd guess you might have missed it when you C&P'd, but it looks like you've a '}' missing off the end to close the method.
 
IMO its easier to spot errors like that by:

Code:
if ( !foo )
{
    //do something
}

As its easier to see you are missing a brace, rather than trying to save lines.
 
I think it doesn't matter, and have experience that has only ever shown private/protected to get in the way, and none to show it save the day, other than pure speculation on behalf of those advocating it.
 
I think it's fine as long as you can justify why you set the visibility of an attribute. Setting everything to public is going to cause you problems sooner or later as you can easily corrupt the state of an object. However if you have a good reason to set it public then fair enough, you might as well make use of the feature.
 
"Corrupt the state of an object" I've never, ever seen that be a problem, simply because no body ever intends to corrupt the state of an object, unless purposely doing so in a test case. :)

And as for "well they might do it unintentionally" the test cases will show this fault very quickly, long before it is even committed to the repository, let alone before it is released or deployed.
 
Back
Top Bottom