Archive for the ‘Java’ Category

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

Convert Inches to Centimetres

Java, Swing 14.10.2011 No Comments
Convert Inches to Centimetres

The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code can be found below.

Read More

Counting numbers

The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and for this we can use…

Read More

Check Palindromes using Recursion

Java 1.6.2011 No Comments

A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends of the String. If they…

Read More

High Low Guessing Game

Java 17.5.2011 5 Comments

The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need is to generate a random…

Read More

Month Calendar

Java 1.5.2011 No Comments
Month Calendar

The following application shows how to format displaying a month in a way that would be suitable for including in a calendar. This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column. The day that is considered the first day…

Read More
s2Member®