Java Examples

Using Scanner to read words from text file

Java, Tip 20.11.2014 1 Comment

Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example of the use of the…

Read More

How to specify log4j configuration in spring application?

Java, Spring, Tip 6.5.2014 No Comments

If you do not want to store your log4j configuration in your classes directory then you need to tell log4j where it can find it. One possibility is to configure it in your applicationContext.xml as shown here If you are running your web application as an expanded war then another option is to use a…

Read More

How can I iterate through all dates in a range?

Java 23.11.2013 No Comments

Ever wanted to iterate through a range of Date’s? We can use Iterator’s for looping through the elements of a Collection, but for Date’s we need to implement the Date arithmetic required. The following class shows how we can create our own Iterator implementation to easily iterate over a range of Date’s, one day at…

Read More

How do I read a text file line by line?

Java, Tip 20.11.2013 No Comments

The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.

Read More

Using a List instead of an array

Java 8.3.2012 2 Comments

Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the triangle. The following code shows…

Read More
s2Member®