WeiJie Lim

Ranch Hand
+ Follow
since Sep 05, 2012
Merit badge: grant badges
For More
Under your bed.
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by WeiJie Lim

Piet Souris wrote:If every line starts with "Gain:" then this will not work, since every key of your Map would be the same. What about the already suggested Map<String, List<Integer>>? That would work, but there would be only one key ("Gain") so you might just as well make a List<Integer>.

But why do you insist on creating a Map? What's wrong with creating a class "Activity" and make a List of these?

As mentioned in my previous post, I have confirmed that it is only the first line which starts with Gain, the rest of the lines starts differently. Each activity is unique. 1 line has a max of 4 activities.
I apologize for the change in format for the data file.
4 years ago
Thanks a lot for all the help so far, I have clarified and the correct data file is in the format

My apologies.

The following code can work, but only on 1 key:value pair per line.

Is it simpler to do it the non-Java 8 way by using regex and string manipulation instead ? I got to admit lambdas and Streams are confusing for me.
4 years ago

Campbell Ritchie wrote:You can probably find a data structure for what you want, but please start by explaining exactly what you want your records to look like.
Don't try arrays, though.

The end result I am trying to achieve is to be able to sum up all numerical values for each activity, and then print it.
Eg based on the 2 lines:

I would want to obtain Gain = 4, Loss = 10 and so on.

I stumbled upon the Pair class, but I am not sure if it is can work..

Apologies, I only have to extract details from the first line, so a Hashmap should be fine.
4 years ago

Campbell Ritchie wrote:Please explain more. Are all the lines in the form “Gain:3:Loss:4:Processed:2:Sent:1:”?
Are you using “Gain” as the “K”? In which case you might overwrite the “V”s and your Map might only contain a mapping for the last line.
Don't declare Streams if you can possibly help it. Once you have used a Stream, it is not possible to go back to it, so it is very unusual to write Stream<XYZ> stream = ...;
What you have in line 13 might be a Stream<String> but in line 14 you are turning it into a Stream<String[]>, so the type will be wrong and line 13 won't compile. You will have to declare it as type Map<...>

Use an IDE to write your code; if you hover your mouse over the method names, you will see a popup telling you the exact return type of each method. Not Stream<T> or anything vague, but Stream<String[]>.
Let's see what you have: In line 13 you are creating a Stream<String[]>, but you have some duplicated code. Don't write Paths.get(...) because you have already executed the selfsame code in line 9. Write filePath.
Line 14 changes the Steam to a Stream<String[]> by correct use of the split() method. If you look here (Java™ Tutorials), you will find that “:” isn't a metacharacter.
You were right to start line 16 with .collect(), but that needs an argument. It is usual to pass a Collector reference; the easiest way to get such a reference being this. Note that will give you a Map<K, List<V>>. I don't know whether that is going to help in you current situation, but at least it will include every line.

Adding discussion to our Streams forrum.

Oh yes, all lines are in the same format, the only difference is in the numerical values.
Oh thanks for pointing out, in that case, Hashmap is not the right Collection to use, my bad. There doesn't seem to be a Collection that can store non-unique key value pairs. Do I have to create a 2D array for this ?
I will try to modify the code to not declare the Stream<String>, thanks
4 years ago
I have a data file where each line in the file is in this format: Gain:2:Loss:5:Processed:1:Sent:0:

I am trying to split each line of the file by the : delimiter and then copying it to a Hashmap where key is the activity and value is the number. Eg: Gain>key, 2>value. The Hashmap is used to easily access the extracted data.

I am using the new Java 8 Streams method to read files, but am stuck at copying to a Hashmap. Guidance is appreciated

4 years ago
Thanks a lot Ron ! That was the key to solving the issue.

On a sidenote, may I know why does transforming from byte[] and String affect the bytes ?
4 years ago
I am facing issues when I send a RC4 encrypted data encoded with Base64 over a Socket.

The decoded base64 tend to not match the source encoded Base64. I have extracted the key issues I face into a small program below.

Any help is appreciated

Sample output:
Plaintext: Hello
Encrypted plaintext in RC4: ���
Encoded plaintext in Base64: F9X6mgs=
Decoded from Base64: ���
Decrypted: H_)K��pd+�

4 years ago
How does the keyword this in the CoffeeSize class refer to the size of the coffee ? I am also confused as to how the CoffeeSize constructor comes into play to determine the cost.


8 years ago
Thanks for the tip, Paul Clapham. I didn't realise that FileWriter does create an empty file if it is not existent.
8 years ago
My code below creates the 2 files successfully, but it is not able to write the sample data into the newly created file. I can't figure out the reason why.

Another strange thing is that when I tried inserting System.out.println calls for debugging, nothing prints out.

Any guidance is appreciated.

8 years ago
I am trying to split the contents of the text file and assign the value on the left of the | separator to a variable and the value on the right of the | separator to another variable. Thus I tried out a sample code to print all the values in the split [] first, and ended up with problems.

This is the content of the text file:
Crazed Boy|20

I keep getting IOException in my sample code, why is this so ? I assumed the split() method is supposed to output for the 1st iteration:
ss[0] = Crazed Boy
ss[1] = 20

Guess I am wrong in my understanding.
8 years ago

Matthew Brown wrote:

Not if you call super(...) or this(...) explicitly yourself. But otherwise, yes.

Oh yeah, thanks for the clarification
8 years ago
Thanks @Matthew Brown and @K. Tsang for your clear replies.

Am I right to say that there is always a 'hidden' super(); in each subclass constructor ?
8 years ago
The LocalStudent class inherits the Student class. The IDE states an error of "no default constructor in Student class". Why is this so ?

I understand that if a LocalStudent object is created, the default constructor of its superclass (aka Student class) will be called implicitly.

But in this case, there is no LocalStudent object being created, so why is the default constructor of Student being called ?

The default constructor of LocalStudent is also overloaded by the created no-arg constructor containining subjects = null; . So there is no call to the superclass default constructor from the default constructor of LocalStudent.

8 years ago
Okay noted, will do so in the future

I managed to solve it, it was due to the messy permissions I have set for the apache folders.

I used a fresh apache folder unzipped from the tar.gz downloaded from Apache's website and everything is back to normal.

Cheers ~
9 years ago