• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Java Problem  RSS feed

 
Ella Lautier
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im creating a database system for a library
so far I have my Book class:



and my useBook class:



in the bookList class, when I enter the line



the compiler gives me the error " ']' expected "
I'm considering changing the array to an array List so i can have a unfixed amount of books in the array
any ideas on whats wrong?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use new Book(...) instead of just the parenthesis when you're creating the book and adding it to the array.
 
Tom Storm
Ranch Hand
Posts: 31
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

From a quick glance it's because you have created an array for books but then you haven't added any. The line below should fix your issue.



TS
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a query/suggestion, does the 'renterId' belong in this class ?
I'm presuming it is meant to indicate if the book has been borrowed or not.

I'm of the opinion that the concept of Borrowing a book rates a class all on its own.

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stefan Evans has suggested... Having the notion of book objects tracking if a book title is available or not is terribly wrong...
There can be many instances of a book title, so if someone is interested in if that title is available or not, you simply want to query if you have any more on the shelf or not...
Taking that into consideration, its the job of a stock tracking component to determine if a title is available or not...
 
Ella Lautier
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Storm wrote:Hi there,

From a quick glance it's because you have created an array for books but then you haven't added any. The line below should fix your issue.



TS


the compiler still gives me the same error im not really sure whats up
 
Ella Lautier
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:As Stefan Evans has suggested... Having the notion of book objects tracking if a book title is available or not is terribly wrong...
There can be many instances of a book title, so if someone is interested in if that title is available or not, you simply want to query if you have any more on the shelf or not...
Taking that into consideration, its the job of a stock tracking component to determine if a title is available or not...



in this case im assuming only one copy of the book is available (quite unlikely i know)
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if you look closely at you're useBook class you'll notice a missing brace { that you need before declaring you're Book array. My first thought was that it was an extra brace but apparently not because that doesn't compile. I suppose since the fields are using each other the compiler needs them explicitly braced.

In any case I believe what you were trying to do was set the useBook class to merely use you're book class where you're program will run. So basically you're just missing your public static void main method.
 
Ella Lautier
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Actually if you look closely at you're useBook class you'll notice a missing brace { that you need before declaring you're Book array. My first thought was that it was an extra brace but apparently not because that doesn't compile. I suppose since the fields are using each other the compiler needs them explicitly braced.

In any case I believe what you were trying to do was set the useBook class to merely use you're book class where you're program will run. So basically you're just missing your public static void main method.



so I should put the bookList array in the main method (because Im using a separate class as the main method)
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ella Lautier wrote:
so I should put the bookList array in the main method (because Im using a separate class as the main method)


Then no. If you're useBook class is essentially a library (you should call it Library) then you're probably best just having an empty library and using the Library class methods to add or remove books in whatever class you have that's using the main method. Another option is to use the Library's constructor to add or remove books. Having the books directly "added" by declaring fields isn't necessarily wrong but it can get tricky and awkward.
 
Piyush Arora
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compiler do you use? I'm surprised it doesn't tell you the line number that causes the error.

In the main class, you need to remove the extra } at the end. Secondly, you initialize an array using {} not (). Let's see if those solve the problem.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!