• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Error in logic

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code:

It prints that greatest is 3, where it should be 7, where did I went wrong?
 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first IF is mostly correct but should take '=' into account as well.

The other should follow the same formula but just swapping the variable names.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never compare second and third (your third if-statement is incorrect).

Here's one way to do it:
 
Saloon Keeper
Posts: 14502
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or for more than 3 values:
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you need concatenate(), Stephan? Let's try without.Thie following might be a more elegant way to deal with a 0‑length input:-I can now see why you used concatenate(): it “forces” the users to pass at least one method argument.

[edit]Why does OptionalInt#orElseThrow() take Supplier<X extends Throwable> as a paremeter type rather than Exception or similar?
 
Stephan van Hulst
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. This is something I learned from Effective Java. When you want to operate on an arbitrary number of parameters, use regular parameters for the required minimum number of parameters and use varargs for the optional additional parameters. That way the compiler will ensure you get enough parameters, rather than having to rely on runtime exceptions.

Campbell Ritchie wrote:Why does OptionalInt#orElseThrow() take Supplier<X extends Throwable> as a paremeter type rather than Exception or similar?


Because you don't want to go around creating objects until you definitely need them. Building exception stack traces may be quite expensive. For the same reason you should prefer Map.computeIfAbsent() over Map.putIfAbsent(), and the Logger method overloads that take a Supplier rather than a String.
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . something I learned from Effective Java. . . .

I shall have to look that up. Thank you. EJ 2nd edition, pages 197‑198.

. . . Because you don't want to go around creating objects until you definitely need them. . . . .

Yes, I remember seeing that sort of thing somewhere, but I have forgotten where.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check it.

 
Stephan van Hulst
Saloon Keeper
Posts: 14502
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that should work. You can simplify it to
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic