• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic