• 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

OCA 8 preparation

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with the following code:




Thanks.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us where such examples come from, to allow us to assess the original, and to reduce copyright problems.
Please tell us what you think is wrong with the code. There are lots of thngs that could be wrong with code. It might fail to compile, it might throw exceptions, it might suffer an infinite loop, or (worst of all) it might silently give a “wrong” answer. Which do you think is happening here? I can see one potential error; are there more?
 
RoderickFuentes Ramos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code came from the book OCP Oracle®  Certified Professional Java®  SE 8 Programmer II
 
RoderickFuentes Ramos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

   I think something is wrong in line 8 of the code.

regards,
Erick
 
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell us why you think that?
 
RoderickFuentes Ramos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for one it lacks close parenthesis...  I added the close parenthesis already but it still doesn't work.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't correct the code from the book; copy it exactly. By all means add a version with the errors you have seen corrected later. Otherwise the errors we find will be different from those shown in the book.
And be sure to tell us exactly what goes wrong.
 
Brecht Geeraerts
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erick,

Whenever you encounter method chaining, beit in a Stream or elsewhere, it is always a good idea to take a step back and reflect on the code. Walk through it one step at the time. What does each method take as a parameter? What does it return? Is that what the next method is expecting? etc.

Also, to avoid confusion: this thread is titled "OCA Preparation" and has been created in the corresponding subforum but please do know that neither Streams nor Optionals are on the OCA exam (but they do appear on the OCP one).
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you managed to write an en‑dash instead of a minus in line 8? Are you using a word processor for editing code?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

second option is taking  Predicate,  not UnaryOperator.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain more, NS.
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ng is referring to the three-parameter version of Stream.iterate.  OP is using the two-parameter version. Note that the three-parameter version is not available in java 8.

@OP
Copy your code into an IDE, and the error will be clear in a second.
 
RoderickFuentes Ramos
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic