Rezha Julio

My name is Rezha Julio
I am a chemist graduate from Bandung Institute of Technology. Currently working as Data Engineer at Traveloka.
You can reach me by email:

, @ Q Q

Powered by Hugo

filter by tags

Java 101Translating Scanner tokens into primitive types

time to read 1 min | 133 words

The Scanner class can be used to break an input into tokens separated by delimiters. Scanner also has methods to translate each token into one of Java’s primitive types.

Delimiters are string patterns and are set in the following way:

Scanner s = new Scanner(input);

If we need to read a file containing a set of integers, and calculate the total, we can use Scanner’s nextInt method:

int sum = 0;
//create a new instance of Scanner
Scanner s = new Scanner(
  new BufferedReader(
    new FileReader("in.txt")));
/*tokenize the input file and 
convert to integers*/
while (s.hasNext()) {
  if (s.hasNextInt()) {
    sum += s.nextInt();
  } else {;

In the example above, we iterate through the file, tokenizing each value and converting it to an integer before adding it to sum.

Java 101Listing a file system's root directories

time to read 1 min | 77 words

Using the static FileSystems class, you can get the default FileSystem (note the missing s) through the getDefault() method.

The getRootDirectories() method of FileSystem can be used to acquire a list of root directories on a file system.

An object of type Iterable is returned, so the directories can be iterated over in the following way:

FileSystem sys = FileSystems.getDefault();
Iterable<Path> d = sys.getRootDirectories();
for (Path name: d) {

This is available from Java 1.7.

Java 101The Console class

time to read 1 min | 110 words

The class provides methods to access the character-based console device, if any, associated with the current Java Virtual Machine. This class is attached to the System console internally.

Console class provide means to read text and passwords from the console.

To read a line as a String:

Console console = System.console();
String myString = console.readLine();

To read a password as an array of chars:

Console console = System.console();
char[] pw = console.readPassword();

If you read passwords using Console class, it will not be displayed to the user.

Keep in mind that this class does not have a high level of security and it is mostly used at development stage.

Java 101Using an interface as a parameter

time to read 1 min | 151 words

You can define methods that take an interface as a parameter. Your interface defines a contract and your methods will accept as parameter any objects whose class implements that interface. This is in fact one of the most common and useful ways to use an interface.

interface Test {
  public void test(); //define the interface

class Tester {
  public void runTest(Test t) {
  } // method with interface as param

MyTest class will implement this interface:

class MyTest implements Test {
  public void test() { // running code }

Now the runTest method will take as a parameter any object that implements the Test Interface:

Tester tester = new Tester(); Test test1 = new MyTest(); tester.runTest(test1);

The collection framework from the standard Java API frequently uses this procedure. For example, Collections.sort() can sort any class that implements the List interface and whose contents implement the Comparable interface.

Java 101Using bounded type parameters in generic methods

time to read 1 min | 114 words

Sometimes it may be appropriate to write a generic method, however it will not be possible for it to accept every type while still maintaining all the necessary functionality.

To solve this, use bounded type parameters to restrict generic methods from accepting arguments of a particular kind.

public <T extends Shape> 
  void drawAll(List<T> shapes){
    for (Shape s: shapes) {

The above method is used to draw a list of shapes. Writing a generic method with an unbounded type parameter would cause problems because lists of other types cannot be drawn in this way.

By specifying that <T extends Shape> we guarantee that only Shape objects can be passed to the method.

Java 101Using the Deprecated annotation

time to read 1 min | 95 words

The @Deprecated annotation can be used to indicate elements which should no longer be used. Any program that uses an element which is marked as @Deprecated will produce a compiler warning.

public void oldMethod() {
public void newMethod() {

In this examples, newMethod replaces oldMethod. However we do not want to remove oldMethod completely because keeping it will help maintain backwards compatibility with older versions of the program.

We can keep oldMethod() and indicate to programmers that it should not be used in new versions by applying the @Deprecated annotation.


  1. java 101 (13):
    Apr 29, 2017 - Translating Scanner tokens into primitive types
  2. python data structure (5):
    May 03, 2017 - Enhance your tuples
  3. python function (2):
    Apr 16, 2017 - Lambda Functions in Python
  4. python generator (4):
    Apr 26, 2017 - Next, Function or Method ?

Friends of Rezha