This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Error in logic

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 719
10
  • Mark post as helpful
  • send pies
  • 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: 10993
243
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 10993
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or for more than 3 values:
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • 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: 10993
243
  • Mark post as helpful
  • send pies
  • 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: 66975
255
  • Mark post as helpful
  • send pies
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check it.

 
Stephan van Hulst
Saloon Keeper
Posts: 10993
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that should work. You can simplify it to
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!