How do I get operating system name and version?

package org.kodejava.lang;

public class OperatingSystemInfo {
    public static void main(String[] args) {
        // The key for getting operating system name
        String name = "os.name";
        // The key for getting operating system version
        String version = "os.version";
        // The key for getting operating system architecture
        String architecture = "os.arch";

        System.out.println("Name   : " + System.getProperty(name));
        System.out.println("Version: " + System.getProperty(version));
        System.out.println("Arch   : " + System.getProperty(architecture));
    }
}

Below is the example result of our program, of course it could be different from what you’ll see in your machine because it depends on the operating system that you use.

Name   : Windows 10
Version: 10.0
Arch   : amd64

Or

Name   : Mac OS X
Version: 10.12.6
Arch   : x86_64

How do I add a delay in my program?

You have a problem that you want to add a delay for a couple of seconds in your program. Using Thread.sleep() method we can add delay in our application in a millisecond time. The Thread.sleep() need to be executed inside a try-catch block, and we need to catch the InterruptedException. Let’s see the code snippet below.

package org.kodejava.lang;

public class DelayExample {
    public static void main(String[] args) {
        // This program demonstrate how to add a delay in our 
        // application.
        for (int i = 0; i < 10; i++) {
            // Print the value of i
            System.out.println("i = " + i);

            try {
                // Using Thread.sleep() we can add delay in our 
                // application in a millisecond time. For the example 
                // below the program will take a deep breath for one 
                // second before continue to print the next value of 
                // the loop.
                Thread.sleep(1000);

                // The Thread.sleep() need to be executed inside a 
                // try-catch block and we need to catch the 
                // InterruptedException.
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }
}

How do I get a part or a substring of a string?

The following code snippet demonstrates how to get some part from a string. To do this we use the String.substring() method. The first substring() method take a single parameter, beginIndex, the index where substring start. This method will return part of string from the beginning index to the end of the string.

The second method, substring(int beginIndex, int endIndex), takes the beginning index, and the end index of the substring operation. The index of this substring() method is a zero-based index, this means that the first character in a string start at index 0.

package org.kodejava.lang;

public class SubstringExample {
    public static void main(String[] args) {
        // This program demonstrates how we can take some part of a string 
        // or what we called as substring. Java String class provides 
        // substring method with some overloaded parameter.
        String sentence = "The quick brown fox jumps over the lazy dog";

        // The first substring method with single parameter beginIndex 
        // will take some part of the string from the beginning index 
        // until the last character in the string.
        String part = sentence.substring(4);
        System.out.println("Part of sentence: " + part);

        // The second substring method take two parameters, beginIndex 
        // and endIndex. This method returns the substring start from 
        // beginIndex to the endIndex.
        part = sentence.substring(16, 30);
        System.out.println("Part of sentence: " + part);
    }
}

This code snippet print out the following result:

Part of sentence: quick brown fox jumps over the lazy dog
Part of sentence: fox jumps over

How do I get operating system temporary directory / folder?

To get the location of temporary directory we can use the System.getProperty(String) method and pass a property key "java.io.tmpdir" as the argument to the method.

package org.kodejava.lang;

public class TempDirExample {
    public static void main(String[] args) {
        // This is the property name for accessing OS temporary directory
        // or folder.
        String property = "java.io.tmpdir";

        // Get the temporary directory and print it.
        String tempDir = System.getProperty(property);
        System.out.println("OS temporary directory is " + tempDir);
    }
}

The output of code snippet above is:

OS temporary directory is C:\Users\wsaryada\AppData\Local\Temp\

How do I create random number?

The java.lang.Math.random() method returns random number between 0.0 and 1.0 including 0.0 but not including 1.0. By multiplying Math.random() result with a number, for example 10 will give us a range of random number between 0.0 and 10.0.

To get a random number between two numbers (n and m) we can use the formula of: n + (Math.random() * (m - n)). Where n is the lowest number (inclusive) and m is the highest number (exclusive).

package org.kodejava.lang;

public class RandomNumberExample {
    public static void main(String[] args) {
        // The Math.random() returns a random number between 0.0 and 1.0 
        // including 0.0 but not including 1.0.
        double number = Math.random();
        System.out.println("Generated number: " + number);

        // By multiplying Math.random() result with a number will give
        // us a range of random number between, for instance 0.0 to 10.0 as
        // shown in the example below.
        number = Math.random() * 10;
        System.out.println("Generated number: " + number);

        // To get a random number between n and m we can use the formula:
        // n + (Math.random() * (m - n)). The example below creates random
        // number between 100.0 and 200.0.
        int n = 100;
        int m = 200;
        number = n + (Math.random() * (m - n));
        System.out.println("Generated number: " + number);

        // Creates an integer random number
        int random = 100 + (int) (Math.random() * 100);
        System.out.println("Generated number: " + random);
    }
}

Here is an example result of our program.

Generated number: 0.024974902600698234
Generated number: 2.271051510086771
Generated number: 147.542543014888
Generated number: 112