Insertion Sort Implementation in Java
Insertion sort is a simple algorithm. Insertion sort logic works as follows: To insert an element in an already existing set, we make room for the new item by moving larger items one position to the...
View ArticleSelection Sort Implementation in Java
Selection sort is one of the simplest sorting algorithms. It is easy to implement but it is not very efficient. The algorithm divides the input list into two parts: the sublist of items already...
View ArticleCalculate Height of any Tree in Java (Non-Binary Tree)
Computing height of a tree in Computer Science is very common. Most of the examples and online discussions talk about computing the height of a Binary Tree. The example method I am sharing can be used...
View ArticleEasiest Way to calculate elapsed time in Java
Quite often in our day to day programming, we need to compute how long does a specific portion of the code takes to complete. For e.g; you might want to check how long does a method take to complete....
View ArticleJava Regular Expression to Validate Social Security Number (SSN)
Here is a utility method to check if a given String is a valid Social Security number using Java Regular Expression. /** isSSNValid: Validate Social Security number (SSN) using Java regex. * This...
View ArticleJava Regular Expression to check if a String is a number
Here is another method from my String utility class. This method uses a regular expression to check if a String is a numeric value. Look at the code, then read through the explanation that follows...
View ArticleHow to delete Workspace from Eclipse Launcher Selection
Launch Eclipse Click on Recent Workspaces Eclipse will list all previously used workspaces Right click on the workspace name that you want to remove Click on Remove from Launcher Selection text. This...
View ArticleHow to generate random alpha numeric String in Java
Following code is an example of generating a random alpha-numeric string using Java Random class. It is designed to generate random String of varying length based on the input parameter. Here is the...
View ArticleJava Scanner class not reading correctly after nextInt() or nextDouble method...
Scanner is a utility class that provides methods to read command-line input. It is a very useful class, but you need to be aware of its unexpected behavior while reading numeric inputs. Consider...
View ArticleHow to compare Strings in Java
One of the most common bugs I’ve seen in Java programs in the use of == operator to compare two String objects. Most of the times the result may be accurate but it is not always guaranteed. We need to...
View ArticleTop 10 Eclipse Keyboard shortcuts for Java developers
Here is a list of 10 commonly used Eclipse shortcuts for Java developers. Ctrl + Shift + T: Open a type (e.g.; a class, an interface) without browsing through list of packages Ctrl + Shift + R: Open...
View ArticleSort numbers in Java to find minimum and maximum values without using Array
Recently a reader contacted me with a question about sorting numbers in Java. After sorting the number the program then needs to print the largest and smallest values. I have written a post earlier...
View ArticleJava String comparison. The difference between == and equals().
Many Java beginners find it difficult to differentiate between == operator and the “equals()” method when comparing String variables in Java. They assume that both operations perform the same function...
View ArticleHow to convert an ArrayList to array in Java
Today, I will share a very basic Java program that converts an ArrayList to array. For most Java developers this is a routine task, but many new comers to Java have asked me this question of how to...
View ArticleHow to validate date using Java regular expression
My earlier post on how to validate email address, SSN and phone number validation using Java regex still attracts lot of visitors. Today I realized that another piece of data that many programmers...
View ArticleHow to write to properties file in Java
One of most visited posts on this blog is How to read properties file in Java. In that post I explained how you can read from a properties file. But many people came to that post searching for an...
View ArticleBest way to check if a Java String is a number.
One of the routine tasks in many Java applications is to convert a string to a number. For instance, you may have a form where user submits his or her age. The input will come to your Java application...
View ArticleHow to determine smallest and largest number in Java
Numbers A couple of weeks ago a reader asked me for a Java program to read command-line input from the user and then display the smallest value the user has entered. He wanted to use digit ‘0’ to...
View ArticleNew features coming to Java 7
Joseph Darcy, the lead of Project Coin, formally announced the approved changes to the Java language to be included in JDK 7. Although there was no major change announced, the announced enhancements...
View ArticleHow to search for a string in a file using Java regular expression.
If you need to search for a phrase or a string in a text file Java regular expression is the easiest way to accomplish this task. Here is a simple example that demonstrates how you can use Java...
View Article