Andrzej Zahorski

Ranch Hand
+ Follow
since Oct 11, 2017
Andrzej likes ...
IntelliJ IDE Python Linux
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Andrzej Zahorski

I got to this point:


But printing every iteration is not efficient, because I am creating multiple Strings in String Pool at this moment I've got no idea how to append it so I will get this inside stringBuilder (example size = 5):
3 months ago

Campbell Ritchie wrote:A Stream‑based solution might be eminently suitable for beginners, but we shall have to make them do it the hard way with a loop (‍) because these exercises are for teaching people how to use loops.


I thought about using Stream API, but it is out of scope for explaining it (I am doing it for another person and he just started learning Java) , but  would you mind sharing Stream solution?
3 months ago
Hi,
Is it possible to create triangle with one instance of StringBuilder in one loop?
This is example of shape of triangle I am  writing about:


I know it is possible to do it with two loops, but I wish to reduce number of iterations.
3 months ago
I do. Hmm, interesting, however if I run program in terminal (not embedded in IntelliJ):


1 year ago
I have problem with reading order which is nothing like what I have in my file.
Could you explain me why is it so?

Here is the beginning of the .csv I am reading:


Here is program that reads it (don't worry about returning null, this is still work in progress, as I need to process that file further on the road)


Here is the output:


However if redirect output of the console to the file - everything looks fine:



1 year ago
I'm trying to write fully functional and optional method that maps optional String -> optional natural number (let suppose natural numbers begin with 0)
This is what I wrote so far:



However my intuition tells me that it is possible to write this function with using only interfaces from Functional library instead using if statements.
1 year ago
Okay, I wrote something like this:


It seems to work.
1 year ago

Campbell Ritchie wrote:You can't iterate a Map. only one of the three sets you can get out of it. You can get access to those three sets via three of the Map's methods. You can use the map() mathod to take the price and count and produce a number. If you have the price as a primitive you may be able to get a Stream of primitives with a method called mapToXYZ() and use its sum() method, otherwise you would have to write a Collector with its three parts yourself, or use the reduce() method which you will find examples of in the package documentation.
Show us what you have tried and tell us how it is going. This is one of the circumstances where it is much easier to use an IDE than the command line.




I was thinking about something like this, however I couldn't put it in code

1 year ago
Hi, I'm trying to remind myself streams and started small project with shopping cart. However I don't know how to get total price of items, which I want to count via Stream API.
How to get into collection, use getPrice() method (method of ShoppingItem, which is the key in HashMap), multiply it by quantity (Value of Hashmap) and get sum of this multiplications?
Here is  shrunk down code:




1 year ago
Sorry for grammar and missing words  - it is a bit late at my place. I'm trying to do one chapter every day, so sometimes it can consume a big chunk of time and energy.
1 year ago

Also I am curious, why do you want to come up with such a method in the first place ?



The only reason that I am making this is, because example in book I'm reading was not interesting for me (Core Java I by Cay Horstmann, Chapter 5). Also I found that making by myself is optimal way to imagine how features work.
This function is first idea that hit my head. Also maybe after extending it it might be useful for debugging purposes - I could pass for example collection of arguments, collection of functions and return collection of output from them. But I'm not really sure  -I am just  Java greenhorn (I think so).

So what would be remedy for not being able to use primitive types? The only thing that comes to my mind is to convert wrappers inside function to primitive types, but it feels like chore.


Also for answer with usage of streams - this is currently ahead of me. I want to use things, that I put into practice so my view of whole core functionalities in Java will be structured.
However it looks like nice hack indeed.
1 year ago
Hi,
I am writing program that is intended to run any method by reflection to check if I understand correctly chapter of book.
For now I am writing part that will run static methods, to focus on smaller parts.
This is how far I am:



The problem is that I get exception:
Exception in thread "main" java.lang.NoSuchMethodException: ch05.my_reflection.ReflectiveMethod.testMethod(java.lang.Integer, java.lang.Integer)

I guess autoboxing do not work here, but I am not sure. Also do you have  any idea how I could make it more functional? I want to be able to  run non-static methods and recieve return values if there is one
1 year ago
Hi, I was thinking about refreshing my knowledge of Java from basic concepts to advanced stuff.
I am considenring Core Java by Cay Horstmann I & II or reading tutorials from site: journaldev.com
I remember that I read about how Strings work in Java (Srting Pool etc) and it was well explained.
Anyone had experience with both? What would you recommend? I've seen posts around here from author of the book, maybe he could share his point of view too.
2 years ago
Thank's I'm adding this website to my blacklist. BTW, do you know any good resource for Core stuff?
I searched many websites and to be honest people do tons of mistakes in them. I read:
* Official tutorials - often lacking in examples
* Jenkov tutorials - webpage - bad practices in code, some of theads examples didn't work.
* Baeldung webpage - good but there's no order in it

The best page I've seen is Journaldev: webpage

Books:
* Thinking In Java - extensive private libraries and Java 7
* Core Java by Cay Horstmann - scope of topics is often wrongly placed - for example in String API guy writes multiple pages about System.out.format - this is not crucial knowledge in programming.


Hi, I am reading a tutorial from webpage about supressed  exceptions and have question about correctness of code:



Shouldn't last catch be after `if ` statement? I I can have access to resource without exception, but closing it can raise one, so can be null in this catch. Hence I think this example is incorrect.