Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, September 23, 2021

Using Regular Expressions in Java

Using regular expressions in Java can be tricky because the input of the regular expression is a String value. This String value must comply to all the Java syntax tricks before the actual regular expression is applied to the String variable being compared.

For example, I want to ensure that the value passed into my Java method is only a whole number. If it's not, then I can't use this value and must use "0" in my method execution. So, here's the regular expression that I have:

.*[^\d].*/g

. - matches any character except line breaks

* - match 0 or more of the preceding token

[ ] - set

^ - negated

\d - matches any digit character (0-9)

/g - apply expression throughout the entire value being compared

However, in Java I must escape the backslash (and remove the global which is assumed in Java) for this regular expression to work. I end up with:

.*[^\\d].*

An alternate solution could have been the following regular expression without having to worry about escaping any chars in Java's string:

.*[^0-9].*

In the end, I was able to use the following and get the expected results:

boolean hasNonDigit = string.matches(".*[^\\d].*");

if (hasNonDigit) setToZero();


Useful Reference and Online Tools





Wednesday, September 16, 2020

Oracle Java 15

Now we have Java 15.
https://blogs.oracle.com/java-platform-group/the-arrival-of-java-15

Thursday, February 21, 2019

Avoid inconsistent Date object definitions

I recently took a pop-quiz question on this, and failed. Errrrr...

Pop Quiz

What is the Date value of the following code?

new Date(2016, 5, 31);

My Answer:

May 31, 2016

Makes sense, right? At least to 70% of us developers who are not JS experts?

See more fun questions at https://app.enkipro.com.

Correct Answer:

June 1, 2016

Since JavaScript early days leveraged a lot techniques and patterns from Java, I looked up both definitions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#Date-int-int-int-
https://www.w3schools.com/js/js_dates.asp

My Further Thoughts

With all the new technologies and programming languages (along with updates to these languages), I'm actually shocked that we haven't provide better consistency with a common class instantiation like the Date object. Particularly, in JavaScrpt.

Since the Time parameters all start from 0, this makes sense and is consistent. However, for the Date parameters, since 'day' starts with 1 and makes the most sense, then why not start the month and the year with '1' as well? Nope, instead, the creators decided to make the month slightly difficult (and very difficult when we add in how fast developers need to work nowadays) by starting the value at '0'. But, no one in the world uses '0' to represent a month, and if it's an array index then define the parameter as such for ALL parameters in the Date definition. If you're not already confused, then try the year with a starting value of '1' and see this is actually 1901. Why, just because the creators somehow thought that programming would only be useful for years 1900-1999 (or shortcut values as 0-99). Thanks guys. Especially to Java guys who decided to just deprecate this and recommend using more code for date formatting (i.e. SimpleDateFormat).
*By the way, I wouldn't complain if these two programming languages weren't so currently popular and in demand.

My Solution

To avoid this unnecessarily confusing definition and prevent making mistakes within my apps, I would create a Date wrapper object that would allow for easier reading of my code. And consistency. That's [programming language] agnostic.

Tuesday, December 4, 2018

JayWay - JsonPath

Today I read a couple articles from TestProject and found this new library quite interesting (when a Maven project uses this library).
https://github.com/json-path/JsonPath

This library helps to format the structure of a JSON file/response.

Wednesday, November 28, 2018

Which Java - since Java's future is at stake?

Many are still talking about the importance of programming in Java. Since there are many apps and systems still running on Java, the need for Java support is still in demand. Or is it?

With Oracle putting the value of Java on the line (and risking its extinction like Solaris), perhaps I need to consider the other options. Especially with Google and community creating Kotlin (as a solution to replace Java). And especially with Amazon now also addressing this concern with its own solution called Corretto (and OpenJDK).

https://aws.amazon.com/blogs/opensource/amazon-corretto-no-cost-distribution-openjdk-long-term-support/

https://kotlinlang.org/docs/reference/comparison-to-java.html

The above link is a nice short comparison of why to use Kotlin over Java. It seems we, the community of developers, always have to wait for corporate to finally release a version which makes application development easier and product performance better. So, instead of waiting for Java <next version>, I think I'm going to try out one of the solutions in this blog.

Now, if I can only get some time to play with these languages. ;-)