+ 1
[Solved] Is there a more elegant way to splitting string and datatype conversion with Java?
I finished Java courses not long ago and trying to solve code coach challenge. When I hit Balconies the question is supplying a comma separated string. And to complete the task those elements need to be converted into integer. The code below is how I solved the problem, but I found it is much cumbersome than python. Is there an elegant way to accomplish the same task? https://code.sololearn.com/cz1tjImWUq2j/?ref=app
3 odpowiedzi
+ 4
In addition to your own solution, which is also perfectly fine, here are some more ideas:
- Stream
- Scanner
- regex (Pattern and Matcher)
Examples in this code:
https://code.sololearn.com/cEYflRWJk6Ac/?ref=app
My code is not a solution for the code coach, but you may find a few more possible ways there, how to compare the numbers and display the results.
+ 3
Tibor Santa
You examples are very helpful, especially the Stream method. It is like "with" in VBA, while inside it is like a lambda function, but in a whole it is like working on a while loop with iterable object.
I think I will save regex for later. I did occasionally use regex in code coach with python, but not often.
0
Wong Hei Ming Java is just inherently verbose. Kotlin is it's cool brother.
I was looking into using lambda expression for your problem, but in Java, it turned out to be longer and more convoluted than your clean solution...😅
Kotlin's lambda and closure is a bit better.
https://code.sololearn.com/cazmgK0k0465/?ref=app