• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array objects creation  RSS feed

 
Greenhorn
Posts: 10
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been for a  considerable amount of minutes grappling with the following code that I have written and it is not compiling because of two errors. Fresh eyes and brilliant minds here, I would be obliged if you could point out where the errors lies. Thank you thank you very much.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've lined up the braces in your code. It should be clear to you now what's wrong. Always use correct indentation in your programs.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clemz Chins wrote:I have been for a  considerable amount of minutes grappling with the following code that I have written and it is not compiling because of two errors.


Can you tell us what the compiler errors are?

Henry
 
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the compiler error is: you have two public classes in the same java file. Or at least, that is what it looks like from your code. These must be separated into two different .java files in your solution. That is, you actually created a nested inner class which I'm sure if not what you intended. You can either:
1) Make it a true nested inner class by removing public
2) Make it a separate class in your solution


Many of these indecies are not correct. To start, myBooks is declared here:

It's declared as Books[3]. This means that there are three possible indicies to use: 0, 1, and 2.
In the above code, you try to access myBooks[3] several times which goes out of range. The max index you can access is one below the specified number of books. If you need a book at index 3, then you should declare myBooks as an array with 4 items.


This looks like a copy paste error. Your indecies go 0, 1, 0, 0. I believe you meant to go 0, 1, 2, 3 (this 3 would go out of bounds).


Why do you re-define myBooks[0] at the end?
 
Clemz Chins
Greenhorn
Posts: 10
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Griggs, Wong and van Hulst. Brilliant and I have sorted it out from your advices. Incredible.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch!
Don't try to create classes without constructors like that. You will also find it much easier if you can use array initialisers. Make sure the Book class (Don't call it Books) has a constructor taking title and author. Then you can fill in the array like this:- More object‑oriented because you are using the capabilities of the Book objects. More reliable and robust because you can be confident the array will be exactly the right size to accommodate all elements, without a risk of nulls or overrunning the bounds of the array. Easier and quicker to write.
If you use such an initialiser otherwise than at declaration time, you will have to insert new Book[] after the = and before the {.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!