java

Scala/Java: How to write a pattern that matches a minimum to maximum number of specified characters

If you’re using Java or Scala and need to write a pattern that matches a range of characters, where those characters occur between a minimum and maximum number of times in the pattern, the following example shows a solution I’m currently using.

The idea is that the pattern "[a-zA-Z0-9]{1,4}" means, “Match a string that has only the characters a-z, A-Z, and 0-9, where those characters occur a minimum of one time and a maximum of four times.” The following tests in the Scala REPL shows how this works:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

A Java method to round a float value to the nearest one-half value

As a quick note, here’s a Java method that will round a float to the nearest half value, such as 1.0, 1.5, 2.0, 2.5, etc.:

/**
 * converts as follows:
 * 1.1  -> 1.0
 * 1.3  -> 1.5
 * 2.1  -> 2.0
 * 2.25 -> 2.5
 */
public static float roundToHalf(float f) {
    return Math.round(f * 2) / 2.0f;
}

The comments show how this function converts the example float values to their nearest half value, so I won’t add any more comments here.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

In Scala, how to get the day of the year

Scala date FAQ: How do I determine the day of the year in Scala?

Solution: Use the Java Calendar class, as shown here:

scala> import java.util.Calendar
import java.util.Calendar

scala> Calendar.getInstance.get(Calendar.DAY_OF_YEAR)
res0: Int = 104

I’m writing this on April 14, 2018, which is the 104th day of the year.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

JavaFX is going to be removed from the Java JDK

Just read that JavaFX is going to be removed from the Java JDK. From the article: “Making JavaFX its own module will make it easier to adopt and clear the way for new contributors, Oracle said. The company added that with the faster release schedule being implemented for standard Java and the JDK, JavaFX needs to be on its own pace driven by contributions from Oracle and others in the OpenJFX community.”

How to write Akka Actors: An example video game

Way back in 2013 — before my first fake heart attack followed by learning that I had thyroid cancer — I thought I was about to go “back to work”, and I decided to try to write another visual demo of Akka Actors before I went back to work. I gave myself 10 hours to write something, and at first I decided to just create some bubbles that would move about randomly on screen. But I got that working so fast that I decided to do something else.

Eventually I came up with the idea of a little “kill the bubbles” game, which turned into a “kill the characters” game. This video shows how it works:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

A Scala “Word of the day” shell script

I have a 19" monitor on the counter between my kitchen and living room, and it’s powered by a Raspberry Pi. I use the Linux Phosphor screen saver to show a scrolling “news and stock ticker” on the display, which I’ve programmed to show news from several different sources (Atom and Rss feeds, along with other news and data sources). An old version of the display looks like this:

My Raspberry Pi news ticker display

Today I added a new “Word of the day” feature to the display, and as with all of the other code, I wrote a Scala shell script to generate the output.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

A Scala method to replace a few “bad” characters

Here’s a little Scala method I wrote to replace some “bad” characters that won’t print properly on my Radio Pi display:

def replaceBadCharacters(s: String): String = {
    s.replaceAll("“", "\"")
     .replaceAll("”", "\"")
     .replaceAll("‘", "\"")
     .replaceAll("’", "\"")
}

There are other ways to solve this problem, but I threw this together as a quick patch until I can figure out why the Phosphor screen saver on the Raspberry Pi won’t print those characters that I’m replacing.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Examples of converting HTML to plain text with Scala and Jsoup

If you ever need to convert HTML to plain text using Scala or Java, I hope these Jsoup examples are helpful:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

How to read Atom and RSS feeds using Scala

In case you ever need to read an Atom or RSS feed using Scala, this example code shows how to use the Java ROME library in your Scala code:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

The keytool password for the Java security cacerts file is ...

In case you ever need to manually a certificate to your ${JAVA_HOME}/jre/lib/security/cacerts file, it turns out the password for that file when using the Java keytool command is changeit.

To add a certificate to that file, you’ll want to use a command like this:

keytool \
    -import \
    -alias "foobar.com" \
    -keystore ${JAVA_HOME}/jre/lib/security/cacerts \
    -file foobar.com.crt

I had to do this today for a Java/Scala script that accesses an HTTPS URL, and the site I’m accessing uses a “Let’s Encrypt” certificate.