Mar 21

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 for different validation that you may require.

  Scanner input = new Scanner( );
  Double value = null;
  while (value==null) {
	  System.out.print("Please enter number: ");
	  String s = input.nextLine();
	  if (s.length()==0) {
		  System.out.println("You did not enter a value; Try again");
	  } else {
		  try {
			  value = new Double(s);
		  } catch (Exception ex) {
			  System.out.println("You did not enter a valid value; Try again");
  double d = value.doubleValue();

written by objects \\ tags: , , , ,

Mar 12

When using non standard fonts in a PDF we need to embed the font so it is available to whoever is reading that PDF (as they probably will not have the font installed on their system).

iText provides a great library for generating PDF’s using Java. It also includes the ability to embed fonts.

If the font is not installed on your system and you instead have a font file that you want to embed then you need to first register the font with iText FontFactory.

Then get the font from the FontFactory making sure to tell it that you want the font embedded.


import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class EmbedFont {
  public static void main(String[] args) throws Exception {

      // Pass name of font file on command line

      String filename = args[1];

      // Register the font

      FontFactory.register(filename, filename);

      // Get the font NB. last parameter indicates font needs to be embedded

      Font font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED);

      // Lets now use the font to create a PDF

      Document document = new Document();
      PdfWriter.getInstance(document,  new FileOutputStream(filename+".pdf"));;
      Paragraph p = new Paragraph("This uses an embedded font", font);

written by objects \\ tags: , , , , ,

Mar 07

Displaytag is a great tag library for rendering data as a table. As well as handling the generation of the html table, it also provides support for easily exporting the table to a variety of formats including Excel (and CSV).

One problem that can occur is Excel formatting the table cells. When it sees a value that looks like a number then it attempts to format it as a number. This can result in values being displayed in Excel in scientific notation when you really want the data to be displayed as is.

To fix this we can use a displaytag column decorator that quotes the values when exporting to Excel. That way excel will see the quoted value and no longer think it as a number and display it without formatting as you require.

import javax.servlet.jsp.PageContext;
import org.displaytag.decorator.DisplaytagColumnDecorator;

public class QuotedExportDecorator implements DisplaytagColumnDecorator {
    public Object decorate(Object value, PageContext pageContext, MediaTypeEnum media) {
        if (media.equals(MediaTypeEnum.EXCEL) || media.equals(MediaTypeEnum.CSV)) {
            value = "=\"" + value + "\"";
        return value;

written by objects \\ tags: , , , ,