Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for Java

Compile Java with External Jar Files at Command Line

If you want to compile Java files at the command line, you use the javac command.

javac ../Java/*.java

This will create a .class file for every .java file that could be compiled.
But let’s say you are referencing one of many external Java libraries that are packaged as .jar files. You probably don’t want to just stick the [...] Read more »

How to Find Interquartile Range in Java

In this post, I explained how to find quartiles in a list of numbers. As a slight add-on to that functionality, you can easily get the interquartile range. This basically means that you are finding the difference between the first and third quartiles.
So I use the code to compute the quartiles and then use simple [...] Read more »

How to Get Quartiles in Java

For a brief overview of what quartiles are, you might read the Wikipedia page on this topic.
Basically, what it means is that if you were to break a list of numbers into four even parts, these would be the values that would separate them. But it gets a little complicated when your list doesn’t break [...] Read more »

Java: Is an Integer an Odd Number?

This is a pretty simple tip, but I still thought I would share it for anyone who is interested. To find out whether an integer is odd, you can use the modulo operator. This operator tells you the remainder after dividing the number by some other number. If you divide any integer by 2, you [...] Read more »

In Java, Use StringBuilder When Constructing Large Strings

This bit of advice is not new to me nor to the software development community. But recently I had one of those experiences where I put together a quick solution (to keep my code as simple as possible), and later I ran into a performance problem. And it was because I was violating this principle.
I [...] Read more »

How to Sort Strings w/ Numbers in Java

In this post, I explain the problem of sorting strings that contain numbers. If you just sort values using the default approach, it will not work properly. For example, it will sort “a10″ before “a2″ even though in your application you may want it to consider the alphabetic characters separately from the numeric ones in [...] Read more »

How to Run an R Script from the Command Line

I’m writing this post because I just spent a couple of hours banging my head against the wall, trying to figure out how to run an R script from the command line. It was working if I simply ran it at the command line. But when I try to run the same command from Java [...] Read more »

Invoke External Application in Java

I recently had a scenario where part of what I wanted to do was in Java, and the other part was in an application that was written in Python. Rather than than rewrite my entire code base in one language or the other, I wanted to find a (quick and dirty) way to invoke the [...] Read more »

Simple Way to Compute Median in Java

There is no way that I know of to find the median of a list of numbers in the Java framework. The median is the middle value. If there is an even number of values, the median is the middle of these two numbers. Below is a method, along with a supporting method and some [...] Read more »

Perform Log Base 2 Transformation in Java

Java has functionality built into it to transform a number using the natural logarithm. This can be done using the java.util.Math.log() method. However, to my knowledge there is no way to do this for base-2 logarithms.
Please don’t let me get started on how silly this is!!
To do a base-2 log transformation:

public static double [...] Read more »

Find the Difference Between Two Dates in Java

Sometimes when you’re writing a program, you want to be able determine the difference in time between two dates. This might be because you want to see how long it took something to run, or you might be processing data and need to find a time span between two events. In searching around the Internet, [...] Read more »

How to Round Numbers (double, float) in Java to Number of Decimal Places

<rant>For some strange reason, Java doesn’t have the ability to round numbers to a given number of decimals. This seems like such an obvious thing to include in any programming language.</rant> Anyway, you can round to the nearest integer, so you have to a little workaround to round to a given number of decimal places. [...] Read more »

How to Override toString() in Python

In popular object-oriented languages such as Java and C#, they have a built-in method to each class that allows you to obtain a String representation of that class. They call these methods toString() or ToString(), respectively. You can use the default implementation, which basically gives you the name of the class (not usually very helpful). [...] Read more »

How to Invoke a Method Dynamically in Python (Reflection)

Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »

Is a Date in a Leap Year in Java

Going back to my very first programming class in 1994, I learned how to design an algorithm that would tell whether a given date was in a leap year or not. We all know that every fourth year is a leap year. However, it gets a tiny bit tricky at the turn of the century [...] Read more »

Get Day, Month, Year from Dates in Java

Please refer to this post for an explanation of how to create Date objects in Java. The code below builds on the code described there. Unfortunately, it takes a little extra code to find the day, month, or year from a Date object. You have to create a Calendar object. Apparently, the Java developers wanted [...] Read more »

How to Compare Strings (Determine Equality) in Java

Most of what you need to know about this is explained in great detail in this post.
The short answer is when you create String objects using the String constructor, you cannot use the == operator to compare those two strings in Java (like you can in C#). You have to use the equals method that [...] Read more »

Find the Difference Between Two Arrays in Java

Please refer to this post for explanation of how to do this using Lists in Java. If you wanted to do the same thing with arrays, you would simply need to convert the arrays to ArrayLists before you find the difference. Read more »

Test Whether a String Can Be Converted to a Double in Java

This is a bit of a hack, but I don’t know of any way to do it more elegantly. You kind of wish the Java developers had gone one more step and included this in their framework, but anyway….
The same type of idea should work for Integers, Longs, Floats, etc.

public static boolean IsDouble(String value)
{
[...] Read more »

Search for Files Recursively in Java

In this post, I explained how to find files using wildcard patterns. This current post builds on the code from that post and allows you to do a wildcard search recursively (in all subdirectories) rather than just in a single directory. Notice that this method calls itself (which is what recursiveness is all about).

import java.util.*;
import [...] Read more »