How do I get user home directory name?

package org.kodejava.lang;

public class UserHomeExample {
    public static void main(String[] args) {
        // This is the key that we used to obtain user home directory
        // in the operating system
        String userHome = "user.home";

        // We get the path by getting the system property with the 
        // defined key above.
        String path = System.getProperty(userHome);

        // We print your home path here
        System.out.println("Your Home Path: " + path);
    }
}

On my machine it print something like:

Your Home Path: C:\Users\wsaryada

Or

Your Home Path: /Users/wsaryada

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 the command line arguments passed to the program?

When we create a Java application we might want to pass a couple of parameters to our program. To get the parameters passed from the command line we can read it from the main(String[] args) method arguments.

To make a class executable we need to create a main() method with the following signatures:

public static void main(String[] args) {
}

This method takes an array of String as the parameter. This array is the parameters that we pass to the program in the command line.

package org.kodejava.basic;

public class ArgumentParsingExample {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + (i + 1) + " = " +
                    args[i]);
        }

        // If you want to check if the number of supplied parameters
        // meet the program requirement you can check the size of 
        // the arguments array.
        if (args.length < 3) {
            System.out.println(
                    "You must call the program as follow:");
            System.out.println(
                    "java org.kodejava.example.basic.ArgumentParsingExample arg1 arg2 arg3");

            // Exit from the program with an error status, for
            // instance we return -1 to indicate that this program 
            // exit abnormally
            System.exit(-1);
        }

        System.out.println("Hello, Welcome!");
    }
}

When we try to run the program without argument we will see the following message:

$ java org.kodejava.basic.ArgumentParsingExample
You must call the program as follow:
java org.kodejava.basic.ArgumentParsingExample arg1 arg2 arg3

And when we pass three arguments we get something like:

$ java org.kodejava.basic.ArgumentParsingExample param1 param2 param3
Argument 1 = param1
Argument 2 = param2
Argument 3 = param3
Hello, Welcome!