Archive for the ‘Java’ Category

Prompt user for console input and validate their response

Java 6.12.2015 No Comments

We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified…

Read More

Printing Triangles in Java

Featured, Java 23.11.2015 1 Comment

A common problem many new Java developers is to write a program that prints out a triangle. There a lots of variations on this problem but lets start with a simple case and then have a look at some possible variations. Probably the simplest case is a left aligned triangle of stars that looks like…

Read More

How do I get a JScrollPane to always display scroll bars?

Java, Swing, Tip 6.11.2015 No Comments

You can achieve this by setting the scroll bar policy on both the verical and horizontal scroll bars.

Read More

How to control decimal places displayed in JTable column?

Java, Swing 10.9.2015 1 Comment

Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells. To control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the parent (DefaultTableCellRenderer) class. To get…

Read More

How to copy a file using Java 6

Java, Tip 10.7.2015 No Comments

Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.

Read More

How do add a checkbox to items in a JList?

Java, Swing, Tip 30.6.2015 1 Comment

We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox’s seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean’s as the cell value (the default…

Read More

Bubble Sort

The Bubble Sort algorithm gets it’s name from the fact that on each pass, one element of the array ‘bubbles’ it’s way through to it’s correct (sorted) position in the array. Rather than me try and explain the details the following video gives a great visual demonstration of how the algorithm works. The source code…

Read More

How to fix “Cannot make a static reference to the non-static method”

Java 19.6.2015 5 Comments

Static methods cannot call non-static methods. An instance of the class is required to call its methods and static methods are not accociated with an instance (they are class methods). To fix it you have a few choices depending on your exact needs.

Read More

Selection Sort

Java 19.6.2015 No Comments

When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It’s not a particularly efficient algorithm and really only suitable for small lists.

Read More

How to check if a Java String is an integer?

Java, Tip 5.6.2015 2 Comments

The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.

Read More