Skip to content

Archive for

7
Aug

Work with Command Line Arguments in Java

Next example shows how to work with arguments in Java. We will input 3 numbers from command line, we will convert those numbers to integer variables and we will print those numbers. Note that parameters in Java are read as strings.

package learn_java;

public class Learn_java {
 
    public static void main(String[] args) {
        int arg1, arg2, arg3;
 
        if (args.length == 3)
        {
            arg1 = Integer.parseInt(args[0]);
            arg2 = Integer.parseInt(args[1]);
            arg3 = Integer.parseInt(args[2]);
 
            System.out.println("Argument 1 is: " + arg1);
            System.out.println("Argument 2 is: " + arg2);
            System.out.println("Argument 3 is: " + arg3);
        }
    }
}
7
Aug

Check if Two Numbers are Within a Range in Java

Let’s say we have two floating point numbers x and y and we want to check if those numbers are between 0 and 1 but not equal with either 0 and 1:

package learn_java;
 
public class Learn_java {
 
    public static void main(String[] args) {
        double x = 0.10;
        double y = 0.20;
        boolean isBetween01;
 
        isBetween01 = (x > 0 && x < 1) && (y > 0 && y < 1);
        System.out.println(isBetween01);
 
    }
}
7
Aug

Print nth character that comes after a known character in Java

Let’s say we want to print the 4th character that comes after ‘b’. In order to do that we will use:

System.out.println((char)('b'+4));
7
Aug

Convert an Integer to String or from String to Integer in Java

Next example will convert an integer (number) to a string in Java:

package learn_java;
 
public class Learn_java {
 
    // convert integer to string
 
    public static void main(String[] args) {
        int number = 20;
        String str1 = number + "";
        System.out.println(str1);
 
        // or:
 
        String str2 = Integer.toString(number);
        System.out.println(str2);
    }   
}

Read more »

7
Aug

Define Basic Variables in Java

In the next example we define basic variables in java. We define an integer, double (floating point number) a boolean and some strings. And then we concatenate two strings in Java. The code is self explanatory.

package learnjava01;
 
public class Learnjava01 {
 
    public static void main(String[] args) {
        int a = 10;
        double b = 20.0;
        boolean isTrue = true || false && true || true;
        System.out.println(a+b);
        System.out.println(isTrue);
 
        boolean isTrue2 = 4.1 >=4;
        System.out.println(isTrue2);
 
        // concatenate strings
        String str1 = "the quick fox ";
        String str2 = "jumped over the lazy dog";
        System.out.println(str1+str2);
 
        int m = 20;
        String str3 = "his age is " + m + " years old";
        System.out.println(str3);
    }   
}
7
Aug

Define an Array in Java and Iterate Through It

Here is how to define an array or a multi-dimensional array in Java and how to iterate through it:

package learn_java;
 
public class Learn_java {
 
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
 
        for (int i=0;i< array1.length; i++ ) {
            System.out.print(array1[i] + " ");
        }
 
        System.out.println("\n");
 
        int[][] array2 = { {1,2,3}, {4,5,6}, {7,8,9} };
 
        for (int i=0; i < array2.length; i++ ) {
            for (int j=0; j < array2[i].length; j++ ) {
                System.out.print(array2[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Read more »