• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

why does the compiler complain about this ternary operator: "not a statement"?

 
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:

Carey Brown wrote:

Anil Philip wrote:The method provided by HackerRank was static and so I had to use static member variables.

Not true. A simple loop, for example, would not have needed static member variables. It could use local variables.


I do not wish to use loops. I want to use lambdas and streams to improve my understanding and facility with them.


Then use Collectors.
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:

Stephan van Hulst wrote:Great, you made it work. Now, never write code like that ever again.

Either use a simple for-loop, or use a collector.


Huh? I am quite proud of what I wrote.



But all of this came from a site called "HackerRank". A "hacker" (before it was somebody who broke into computer system) historically was somebody who wrote quick and dirty code so they could move on to the next hack. Neither version of the word is a compliment. And neither version of the word is anywhere close to describing a professional programmer.
 
Saloon Keeper
Posts: 15276
350
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice Stephan. Hackerrank only goes up to Java-15 though and "record" was introduced in16. But I did copy your code and some test input samples into a sandbox and it works and is elegant.
 
Ranch Foreman
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Anil Philip wrote:. . . Can you please share your code using collectors? . . .

I already have.



I missed that. It works without member variables when you use Collectors. I had never heard of signum() before. Thanks.

 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . .

I told you that calling a List arr was a bad idea. It has confused you and that code won't compile because you are using Arrays.stream() as I did whan I thought it was an array.
If you find the most dubioius classification of methods every seen, you will find that newest version of the method is a 1468; (1) it takes information from the oiutside world (its parameter), it returns nothing, (4) it returns nothing, (6) it doesn't read any fields, and (8) it doesn't write to nor modify any fields. It is now a legitimate candidate for being a static method.
* * * * * * * * * * * * * *
By the way: if HackerRank says only to modify the plusMinus() method, doesn't that mean you aren't allowed to add fields to your class? And I think plusMinus() is another poorly‑chosen name.
 
Bartender
Posts: 5466
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OP uses arr.stream(), and since arr is a List, that code will work    

For the sake of some (not very efficient) alternatives:
 
Anil Philip
Ranch Foreman
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . you aren't allowed to add fields to your class? . . .


Please go to the Hacker Rank website. They gave the method signature. Since all the 3 approaches worked and passed their test cases, they are apparently okay with adding the member variables.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice, Piet, but please explain a bit more in case OP isn's familiar with a Collector like “all Gaul,” “divided into three parts.”
Would you have any  misgivings about precision if you are doing multiple floating‑point additions?
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:Since all the 3 approaches worked and passed their test cases, they are apparently okay with adding the member variables.


Unit tests do not check for coding style.

The fact that something works, does not mean that it is also clean, maintainable code.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . The fact that something works, does not mean . . . .

It also doesn't mean it is correct code; the tests might have missed a corner case for which the code fails.

How do you do unit testing on something with void for its return type?
 
Anil Philip
Ranch Foreman
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Anil Philip wrote:Since all the 3 approaches worked and passed their test cases, they are apparently okay with adding the member variables.


Unit tests do not check for coding style.

The fact that something works, does not mean that it is also clean, maintainable code.



Hacker Rank expects you to complete each of these problems within 10 minutes. (which is why I never pass these type of coding interview tests)
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . I never pass these type of coding interview tests

You need lots more practice. Show us some more code and let us improve it for you
 
Piet Souris
Bartender
Posts: 5466
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nice, Piet, but please explain a bit more in case OP isn's familiar with a Collector like “all Gaul,” “divided into three parts.”


OP posted another topic about how to get a frequency table, where OP got some clear answers. So if he (or she) likes an explanation, then (s)he can ask for it.

Campbell Ritchie wrote:Would you have any  misgivings about precision if you are doing multiple floating‑point additions?


Yes indeed, But it is sufficient to get the first four decimals oke.

My intention was to draw attention to some other possibilities: merge and summingInt (or here: Double);
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, there should be no problems with precision. Floating point values can represent integers exactly, so summing them should lead to no loss of accuracy or precision.

Other than simply demonstrating the existence of the collector, there's still no good reason to do it, but that's another matter.
 
Master Rancher
Posts: 4661
63
  • 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 wrote:In this case, there should be no problems with precision. Floating point values can represent integers exactly, so summing them should lead to no loss of accuracy or precision.


But that's not true of Piet's code:

Piet Souris wrote:


(Bearing in mind that Piet said this was not very efficient in the first place, but offering alternatives for the sake of learning.)

In this case, the "frac" is clearly subject to rounding... though as Piet notes, it's more than good enough for the required precision.

In the spirit of beating a dead horse , here's a version that would be better for rounding. Not sure if it would end up being more or less efficient... probably about the same:
 
Marshal
Posts: 8831
631
Mac OS X VI Editor BSD Java
  • 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 wrote:


Nice. With one assumption. It would fail with inputs such as: List.of(); List.of(-1); List.of(0); List.of(1) and pairs of them.
 
Mike Simmons
Master Rancher
Posts: 4661
63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not just pairs - it would fail on List.of(1,2,3) and many other possibilities.  But, it has a fairly simple solution.  (Several, really.)
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, I should have used getOrDefault().
 
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 22 (and 21 with preview features enabled) allows you to do this:

By assigning it to a variable it becomes an expression. But _ (disallowed since Java 9, reallowed since Java 22) is an unnamed variable - it cannot be used anywhere, but serves as recipient of a value you discard. You won't get a warning that the variable is never used.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:. . . reallowed since Java 22 . . .

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah! So that is what _ was reserved for. I knew it was some sort of unnamed variable but had never seen it used.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic