BASIC / NOTES</>↗Basic18 Sep 2009
The if-then-else control flow statement adds a secondary path to the if statement when the expression evaluates to false. When it evaluates to false the else block will be executed. Below is the program that takes input of user test score and evaluates the score to get the corresponding grade. package org.kodejava.basic; import java.io.Console; public […]
BASIC / NOTES</>↗Basic17 Sep 2009
The if-then is the most basic control flow statement. It will execute the block of statement only if the test expression evaluated equals to true. Let’s see the example below: package org.kodejava.basic; public class IfThenDemo { public static void main(String[] args) { int a = 10; int b = 5; // If the evaluation of […]
BASIC / NOTES</>↗Basic15 Sep 2009
package org.kodejava.basic; import java.util.HashMap; import java.util.Map; public class NumberOccurrenceInArray { public static void main(String[] args) { int[] numbers = new int[]{1, 8, 3, 4, 3, 2, 5, 7, 3, 1, 4, 5, 6, 4, 3}; Map<Integer, Integer> map = new HashMap<>(); for (int key : numbers) { if (map.containsKey(key)) { int occurrence = map.get(key); occurrence++; […]
BASIC / NOTES</>↗Basic13 Sep 2009
The double brace initialization {{ … }} is another way for initializing a collection objects in Java. It is offer a simple syntax for initializing a collection object. package org.kodejava.lang; import java.util.ArrayList; import java.util.List; public class DoubleBraceInitialization { public static void main(String[] args) { // Creates a list of colors and add three colors of […]
CORE API / NOTES</>↗Core API12 Sep 2009
This example show you how to count the number of a character occurrences in a string. We show two ways to do it, using the String.replaceAll(String regex, String replace) method and creating a loop that check every char in the String and count the matched char. package org.kodejava.lang; public class CharCounter { public static void […]
APACHE COMMONS / NOTES</>↗Apache Commons07 Sep 2009
We can use the code below to convert the content of an InputStream into a String. At first, we use FileInputStream create to a stream to a file that going to be read. IOUtils.toString(InputStream input, String encoding) method gets the content of the InputStream and returns a string representation of it. package org.kodejava.commons.io; import org.apache.commons.io.IOUtils; […]