Kotlin: A for loop that counts up to some maximum integer value

As a brief note to self, I was just converting some Java code to Kotlin, and the correct way to convert this Java for loop that uses i as a counter:

for (int i=0; i<tabLayout.tabCount; i++) { ...

is with this Kotlin for loop:

for (i in 0 until tabLayout.tabCount) { ...

The key there for me is the 0 until part of the syntax.

How to create a range of characters as a Scala Array

I just noticed this quirk when trying to create an array of characters with the Scala Array.range method:

# works as expected
('a' to 'e').toArray              // Array[Char] = Array(a, b, c, d, e)

# surprise: Array.range always returns Array[Int]
val a = Array.range('a', 'e')     // Array[Int] = Array(97, 98, 99, 100)

I was surprised to see that the Scaladoc for the Array object states that the second example is expected behavior; Array.range always returns an Array[Int]. I suspect this has something to do with a Scala Array being backed by a Java array, but I didn’t dig into the source code to confirm this.

For much more information about arrays, see my Scala Array class examples tutorial.

How to get the current year as an integer in Scala

Scala FAQ: How do I get the current year as an integer (Int value) in Scala?

Solution: Use the Java 8 Year or LocalDate classes, or the older old Java Calendar class. The solutions are shown below.

How to create a Java or Scala date from a Long value

Scala FAQ: How do I created a Scala Date from a Long value? When I give the Java Date class constructor a Long value it returns a date from 1970.

Solution: You need to multiply the Long value by 1000, and also make sure you pass a Long value into the Date constructor. The Scala REPL shows how this works:

Scala: How to get the current month as a number or string

Table of Contents1 - Get the current month as an integer number2 - How to get the current month as an abbreviated string3 - How to get the full month name4 - A note about SimpleDateFormat and Locale5 - Summary

Scala FAQ: How do I get the current month as an integer or as a string in Scala?

Scala for Java devs: Everything in Scala is an object

The new docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).

A Java method to determine if an integer is between a range

Last night I was writing some code for my Android football game, and decided it would be best if I had a between method, so I could write some code like this to show that I wanted to test to see if a number was between an integer range:

if (between(distance, 8, 10)) { ...

That could would be interpreted as, “If the distance is between the values 8 and 10, do whatever is in the code block.” (I can make that code more readable in Scala, but in Java I think that’s the best I can do.)

Java: How to round a float or double to an integer

Java math FAQ: How do I round a float or double to an integer in Java?

Solution: Use Math.round() to round a float (or double) to the nearest integer (int) in Java.

You can see how this works in the examples that follow, where the result is shown in the comment after each line:

Safe String to Int conversion in Haskell (a string from the command line)

I was working on a Haskell factorial function (which turned out to be easy) and decided I wanted to write a program to prompt a user at the command line for an Int, and then do the factorial of that Int.

I quickly learned that this required a String to Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a String to Maybe Int conversion, as you can see in this code: