File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B SCJP 1.6 book -> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B SCJP 1.6 book -> "collections" : error in code ?" Watch "K&B SCJP 1.6 book -> "collections" : error in code ?" New topic
Author

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

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 :


SCJP 6. Learning more now.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

The root cause is- there's a missing bracket for the compare method


Mohamed Sanaulla | My Blog
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Sep 08, 2007
Posts: 3056
    
  33

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

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B SCJP 1.6 book -> "collections" : error in code ?
 
Similar Threads
Priority Queue
Generics
Collections..
priority queue!
Regarding PriorityQueue