Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B SCJP 1.6 book -> "collections" : error in code ?

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copied the code from page 591 and complied it and got errors. What is the problem ?

This is the code :


THE DESIRED OUPUT as per the book :


BUT THE ERRORS I GOT :

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the benefit of the reader, i have added the explanation of the program (as per K&B) below :

Let's look at this in detail. The first for loop iterates through the ia array, and uses the offer() method to add elements to the PriorityQueue named pq1. The second for loop iterates through pq1 using the poll() method, which returns the highest priority entry in pq1 AND removes the entry from the queue. Notice that the elements are returned in priority order (in this case, natural order). Next, we create a Comparator—in this case, a Comparator that orders elements in the opposite of natural order. We use this Comparator to build a second PriorityQueue, pq2, and we load it with the same array we used earlier. Finally, we check the size of pq2 before and after calls to peek() and poll(). This confirms that peek() returns the highest priority element in the queue without removing it, and poll() returns the highest priority element, AND removes it from the queue. Finally, we review the remaining elements in the queue.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:I copied the code from page 591 and complied it and got errors. What is the problem ?


It looks like you didn't copy the code correctly -- those are syntax errors. For example, the first one is caused by missing braces.

Henry
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The root cause is- there's a missing bracket for the compare method
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
It looks like you didn't copy the code correctly -- those are syntax errors. For example, the first one is caused by missing braces.
Henry


got it, sorry my mistake. But how do i interpret compile and run-time errors (except the simple ones). Is there any manual/trouble shooting guide for this, for example if you get xyz error - in plain english your problem is abc...

line 6 to be replaced by


thanks
rb

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should can always go down the compiler error messages you get, fixing one by one. I saw that in the Java7 the readability of the compiler messages have been improved.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,

The other thing to consider is how you are formatting your code. In the code you posted your use of indentations is inconsistent. To some degree editors can help you find bugs, and to some degree IDEs can too, but it's also good for you to format your code so that when you (or other programmers) look at it later, it's as easy to read as possible. As an example you had something like:

for(....)
stmt1;
stmt2;

Without curly braces, only stmt1 will be a part of the for loop. Because you didn't use curly braces AND you didn't indent stmt1, this is an easy bug for the eye to miss. There is another discussion about whether you should ALWAYS use curly braces, but I'll leave that for another day.

hth,
Bert
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:You should can always go down the compiler error messages you get, fixing one by one. I saw that in the Java7 the readability of the compiler messages have been improved.


Where can i see examples of this ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic