Archive for the ‘Java’ Category

Setting print margins using Eclipse to maintain line character length

Friday, May 14th, 2010

Sometimes you may have standard line lengths with which to work when writing code. If this is the case and your using Coldfusion and Eclipse with the cfEclipse plugin you can maintain the character length consistency by setting up the print margins to n character length.

Point being that when running the cfeclipse plugin you need to choose CFEclipse > Editor in the preferences left navigation. In the options you will check ‘Show print margin’, set the ‘Print margin column’ text field to the desired character length, and if you choose the ‘Print margin’ option in the ‘Appearance color options’ select box. Then choose the color you wish the margin to be. This will set a line length that will show in the code editor pane. Of course it will not wrap the line. It is still your duty as a developer to wrap appropriately. Nonetheless it does give you a guide for where the line should wrap at least.

If you’re writing Java then you would select General > Editor > Text Editors in the preferences pane.  Although the display is slightly different, generally follow the same steps as above for showing the print margin.

If you’re using Coldfusion Builder you will follow the same exact directions as listed just above for showing the print margins writing Java.

Lastly, if you’re using Flex Builder 3 you will again follow the same directions as above for showing print margins writing Java.

Converting a string to an array of characters

Monday, March 8th, 2010

Just thought I’d write up a quick post on converting a string of characters to an array of characters.  I’ve never had any difficulty doing this using Java’s getBytes() method which converts a given string to an array of it’s equivalent byte code.  Nonetheless I just stumbled upon a native Coldfusion method that does the same exact thing without having to directly access the underlying Java.

To convert a string to an array of character bytes using Coldfusion simply use the charsetdecode() method.  You can then loop over the byte array and encode each character using the chr() method, and set into a new array.  In the end you will end up with an array of the characters including white spaces and all.  Also any string you pass through the charsetdecode() method you can pass back through the charsetencode() method to convert it back into its original string.  A quick example of this is below.

<!--- SET A STRING --->
<cfset VARIABLES.myname = "My name is Matthew.">

<!--- CONVERT TO AN ARRAY OF BYTES --->
<cfset VARIABLES.bytes = charsetdecode(VARIABLES.myname, "us-ascii")>

<!--- CREATE AN EMPTY ARRAY --->
<cfset VARIABLES.data = arrayNew(1)>

<!--- LOOP OVER ARRAY OF BYTE CODE --->
<cfloop array="#VARIABLES.bytes#" index="VARIABLES.i">
	<!--- SET EACH CHARACTER INTO THE ARRAY --->
	<cfset arrayAppend(VARIABLES.data, chr(VARIABLES.i))>
</cfloop>

<!--- DUMP THE ARRAY --->
<cfdump var="#VARIABLES.data#">

It outputs the following.

Coldfusion Character Array

It can now be treated as any array. What else have I been overlooking?

Accessing a query resultset by row number

Tuesday, January 12th, 2010

I ran into a little issue this morning of needing to access a query resultset not by column or item id or anything usual.  Instead I needed to access data in the query via the row number.  This is not something that occurs often for me.  In fact this is the first time I’ve encountered this.

I checked Adobe’s docs and found no method immediately accessible for getting a row of my choice.  The closest I found was the currentRow() method that can be used when looping through a query.

Knowing Java has a slew of methods for working with query result sets I turned to the java docs.  Sure enough I found a way to use java methods to return specific data about a row of my choice.  Afterwards, I found on Ben Nadel’s blog a post about converting a query into an array.  He posts there a way of accessing the query object as a struct.  So let’s take a look at a quick example of how to use both.

First let’s create a query to work with.

Create Query

<!--- CREATE LIST --->
<cfset VARIABLES.listofcolors = "blue, red, purple, green, yellow, magenta">

<!--- CREATE A COUNTER --->
<cfset VARIABLES.counter = 22>

<!--- CREATE QUERY --->
<cfset VARIABLES.getQuery = querynew("id, colors")>

<!--- LOOP OVER COLORS --->
<cfloop list="#VARIABLES.listofcolors#" index="VARIABLES.i">
	<!--- CREATE A NEW ROW IN QUERY --->
	<cfset queryaddrow(VARIABLES.getQuery, 1)>

	<!--- ADD DATA --->
	<cfset querysetcell(VARIABLES.getQuery, "id", VARIABLES.counter)>
	<cfset querysetcell(VARIABLES.getQuery, "colors", VARIABLES.i)>

	<!--- INCREASE COUNTER BY 1 --->
	<cfset VARIABLES.counter = VARIABLES.counter + 1>
</cfloop>

(more…)