Mark Richardson

Ranch Hand
+ Follow
since Dec 28, 2016
Cows and Likes
Cows
Total received
2
In last 30 days
0
Total given
0
Likes
Total received
10
Received in last 30 days
0
Total given
79
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mark Richardson

Paul Clapham wrote:And is it really necessary to pass the timestamp to that method in two separate parts? If you're stuck with that, then the first thing you should do inside the method is to combine them into a single string which looks like "11/01/2020 05:05 PM". Then you can easily create a pair of formatters to transform that to the SQLite standard form.



Yasssssssss! :D So simple and right under my nose. Thank you! and Thank you to Campbell Ritchie for pointing out the obsolete Date library I was using.
1 week ago

Campbell Ritchie wrote:

Mark Richardson wrote:. . . //apparently, I can have one or the other... I can't set time and date separately . . .

No, you are creating two different objects and when you create the second you are discarding the first.
Which Date class are you using? You know how useless java.util.Date is? Remember that it comes with setXXX() methods which you can probably use for what you want. Find out some 21st century date classes: look here in the Java™ Tutorials.
If you want a String, why not use a class designed for manipulating Strings: this, for example.



As instructed, I am now using the `java.time` library. However, right away, I receive an exception where "05:05 PM" could not be parsed.




1 week ago
I've created a method `createDateTime` which takes 2 parameters. The first parameter will be a date in String format (American style - MM/dd/yyyy).
The second parameter will be time in AM/PM style.


The problem I am running into is as follows: When I use `DateFormat` to parse my `time` input parameter, I surely get a date object, with the time set, but as soon as I try to set `date` on it, the time gets overwritten!

Ultimately, I want to return a generate/return a string which I will write to an SQLite database, in this format:
"YYYY-MM-DD HH:MM:SS.SSS"

I'll be happy if I can return a Date object first though... and then I can worry about turning that into a String...






1 week ago
This is anecdotal, but I worked on a large JSF based project and we used Oracle JDK 8 on Eclipse, with project compatibility set to 7. One day, I decided to use a newer Oracle JDK on eclipse (I think it was 9 or 10, but set to compatibility for 7 to support the project) - the project simply wouldn't run. Disappointed, switched back to JDK 8.

Then, later, on a completely different project, we were told to use OpenJDK, and told that Oracle JDK will not work properly.

What's the story on this? The first scenario I mentioned shouldn't even exist, because a Java developer is taught from the get-go that "Java is backward compatible." Yet... here I am... seeing that it's not?

And then we're told that OpenJDK version x,y,z should support Oracle version x,y,z - but that doesn't seem to be the case either...

Is it a fair statement that "in the wild" you can expect abberant results?
1 week ago
I wrote the following method, but I'm not entirely sure if this is the correct way to implement a prepared statement... I don't feel entirely comfortable with it. I thought that I would have to implement "?" (question marks) which would tell `ps.setString(1, nickname);` which item in the query I am referring to...

2 weeks ago

Junilu Lacar wrote:You don't even have to use regular expressions for this.

The convert() method would also report any errors it finds but those would be violations of whatever formatting rules you choose to implement rather than illegal numerals.



Today I learned: Not to ignore Java Streams and actually learn them! :P
1 month ago

Carey Brown wrote:If you want to be even more robust you can look  for patterns of 4 or more of any of those characters that appear consecutively, which should never happen. Example: "VIIII".

Pattern: "I{4,}|V{4,}|X{4,}|L{4,}|C{4,}|D{4,}|M{4,}"
Now  with that pattern you DO want to use find() because you don't care where in the string the pattern occurs.



Hi Carey, so I tried this:



However, it's taking the following (wrong) inputs:
P and returning 0
and IP and returning 1.

I thought that the regex would ensure that upon encountering a "P" it would print "Invalid Input" and error out.
1 month ago

Junilu Lacar wrote:We could speculate and debate about what is and isn't valid but since we haven't heard from OP since the opening post, I don't think any of it matters until we hear what his requirements are. On the subject of clear requirements for this problem, this article might be of interest: https://dzone.com/articles/roman-numerals-kata-tdd-and



I would defer to the "specification" which Piet posted from ProjectEuler: https://projecteuler.net/about=roman_numerals
I wrote my program "for fun," and now I'm realizing the uncomfortable position I've put myself in.. haha - I suppose one could just copy and paste this solution on Stack overflow.
https://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression

Here is my ENTIRE code btw...  and currently the does not work as expected...



1 month ago
As we all know, pattern matching is not straight forward in Java. There's , then there's , and then there's this method...
It's a little beyond me, to be honest. I created a small program which takes in Roman Numerals, and converts them into Arabic Numerals.

I thought it would be nice to have validation on my inputs, so I cared this method:



What I am trying to do in plain English: the validate method makes sure that the String I enter only accepts Roman Numerals which are composed of the letters I, V, X, L, C, D, and M.
Input which should pass:
XX
IX
XI
IV
V
I

Input which should NOT pass:

PP
P
XP
IG

However, when I enter XP, it still recognizes this as "10" and does not catch the "P". When I enter PI, it does not catch the "P" but recognizes the I and gives me a "1"
What am I doing wrong? I think my REGEX pattern is wrong.
1 month ago
Thank you for that line by line explanation!
7 months ago

Junilu Lacar wrote: See if you can understand what this does:


Yes, I believe I understand both of these implementations. Here's my understanding:

In the first one, we created an object which we assign the reference variable: byLength.
This object is of type Comparator<String>, therefore, it must override the compare method because
the Comparator interface contains the compare method.
Our custom implementation of the compare method will leverage the static method of the Integer class and compare
the lengths of the 2 strings we pass to it.

Similarly, in the second example, we will compare based on the hashCode of the String.

What eludes me is what happens to these the objects when it comes to actually using them. i.e. passing "byLength" and "byHashCode" as arguments.
In other words let's assume there exists a method called "someMethod()." How will this hypothetical method actually utilize "byLength" as an argument?

e.g.

What will the declaration of this "someMethod" look like?
7 months ago
Let us consider the following code:



I'm shaky on my understanding of this expression, so I will attempt to dumb it down. In English, we would say:

"Hey Mr. Compiler! I want to create an object called "comparator," which is of type "Comparator<String>".
The -> (arrow operator) is simply shorthand for  the curly braces and semi-colon to denote the anonymous class and the return statement.
In the past, we would have had a "compare" method inside an anonymous class body, which took 2 parameters: "String s1, and String s2", but this was redundant, since we
are only interested what this anonymous method is returning. So, in essence, we are now also declaring an anonymous method!

So now, the above code will give us an object called comparatorObject, which will primarily (exclusively?) be used as an argument to some method. How? why? I'm not sure... but I imagine
that this method which is expecting to receive comparatorObject as an argument, "knows" that these comparatorObjects contain anonymous methods, which will give a return type, which it will do something with.

Am I right on all points? I still feel I'm lost on how this comparatorObject will be "consumed" by the receiving method, so if someone can please dumb it down for me and explain it step-by-step in the above style? Thank you!


7 months ago
Let us consider the following code:



Here is my rudimentary understanding of what this code does:

Hey Mr. Java compiler! Please give me a new object of type "Comparator<String>"
Oh, btw. Mr. Compiler, I will be embedding(?) a method into this object. This method will accept 2 arguments, both of type String.
I will use the Integer class's static method, "compare" to compare these 2 Strings which I pass into this method.
Now, I have this magical comparatorObject object, which can now be used to pass as an argument into methods!

This last part really confuses me. Traditionally, a method takes  parameters which are of a particular data type. The code within the method *does something* with that passed argument.
However, in the case of anonymous classes, is it a true statement that there are certain methods which are now expecting objects, which contain a particular method, which will return some value. Is this a concise statement which explains precisely what an anonymous class containing a method is doing? or am I missing something?
7 months ago
In my code, I have 2 methods. In my first method, I completely rely on StringBuilder and it's reverse() method. This seems to work. However, in my second method, I don't get anything returned. Why is this?


11 months ago
So, as a student, I've always learned that we make our instance variables private because some "rogue class" might inadvertent (or nefarious) changes to your class.
That is, every class should have responsibility over it's own variables. I get that.

But the getter and setters methods are public. They can presumably just be called by another class, no?

And what exactly is a "rogue class" anyways?

I would imagine that when I am working on a project, I have access to each and every class that is on that project.

Where is this supposedly "nefarious" activity taking place?

I know that we use access modifiers to encapsulate our variables so that only the appropriate classes are able to have access to them... but in the event that another class DID *god forbid* get access to some variable... the programmer would know about it, no? After all... the programmer is able to see the entire code on his/her project...

I don't understand the real-world implications of this
1 year ago