Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Comments

Intro to Spring Security Core for Grails
Eric Pierce said: Thanks for these screencasts, Dan! You made it crazy simple to get up and running w/security core h... [More]

Removing duplicates from an array of objects
Arvind said: Great tip, can't thank you enough for this. [More]

Intro to Spring Security Core for Grails
Santosh said: Thanks much for putting up these screencasts. As the others here I'm a beginner and I've been having... [More]

Grails Spring Security Plugin - Logout postOnly setting
eriihine said: I still had some issues with this one. It seems that the href link is always generating a GET method... [More]

Intro to Spring Security Core for Grails
Dan Vega said: Just a heads up but I decided to write up a quick post on your question just in case it trips up any... [More]

Chrome Developer Tools: Tips & Tricks from Paul Irish

I found this video the other day and thought I would share it with you guys. In this video Paul walks through 12 tricks to becoming a quicker developer. If you use Chrome and find yourself living in the developer tools this is a must watch. If you already know about all 12 of these then you are ahead of the game but I am guessing you will learn at least 1 new thing from this video so enjoy.!

 
 

Day Month Year Java Exercise Part II

If you didn't get a chance to check out part I please read it first so this post makes a little more sense. My friend called me and said that he was done with his exercise and since it wasn't due for another 2 days he was going to try for extra credit. The extra credit was to have the program flow a little bit better and had to meet the following requirements.

  • When the user clicks the cancel button the program must exit
  • When the user enters something that doesn't meet our validation they must be prompted to re enter that data.

First off we need to solve this cancel button riddle. It happens to be a pretty simple answer. When you take input from the user and they click cancel, null is returned. So we can check for null and just exit the program.

Now the next requirement is a little bit trickier. First off this program is impossible to write the way its currently constructed. If we ask for the day first there is no way for us to validate at that point if what they are entering is correct. Now if we ask for the month first then we can set our max days variable. So in this case to make it actually work I am going to ask for the month, day and then year. Now to accomplish this we have to use a while loop. What we to do is keep asking for the users input while a condition is true. In the case of our month we can do the following. What we are saying is while m is 0 (default setting) and greater than 12 (invalid month) keep asking for the input.

Pretty simple but if you haven't worked with swing in awhile (this guy) it can be tricky. Here is the complete example.

Update:

I just realized that our leap year requirement would not work the way I wrote it. The only way for this example to work correctly is to ask for the info in the exact opposite way the exercise wanted it. You need to know the year so that when you asking for the month you can set the correct number of max days and you need to know what the max days are when you are asking for the day.

 
 

Day Month Year Java Exercise

A friend of mine is taking some programming classes at a local community college and I asked him to forward me some of his exercises just for fun. I understand that these mini tests are supposed to teach you programming fundamentals but the veteran in me can't help but notice how unpractical they are and therefor tend to over think them.

Exercise

This week we are going to write a program that accepts a day, month and year. The program will then output the date in a readable format. So for example if the user enters 20,1,2011 the program will output January 20, 2011. Seems fairly simple but the requirements make it a little harder to take advantage of the Java API.

Requirements

  • The user input needs to be implemented with Dialog boxes in a GUI fashion.
  • There needs to be error checking on the input. For instance, the month number should be between 1 - 12, if not a user friendly error is submitted back to the user. Be sure to check the number of days in a month and the year for valid ranges.
  • A switch statement needs to be implemented to convert the month number into the month name. Also, the switch logic should define the max allowable days in a month based on what month it is (e.g. January would have a max days of 31, whereas April would only have 30).
  • The program needs to check if the day indicated is a leap day (i.e. Feb 29th, in a leap year). If so the user output needs to indicate that it is a leap year. The code for leap year calculations can be found in 3.13 on page 90. Please note: the input parameters of 29, 2, 2012 Needs to be at least one of your data sets, and shown in your output file as indicating a leap day ... within a leap year (Remeber this is two different checks).
  • Finally, the output needs to be a GUI box back to the user indicating the text representation of the date, with indications of a leap day and leap year if found.

With those requirements here is what I came up. Now I realize that I am violating one of the rules here by not setting max number of days in the switch but that is what the API is for, to make my life easier. Maybe I would get extra credit :)

The only problem I have with it is there is no real logic way to trap for bad input. If a user enters a string an exception will be thrown. You could trap that exception but the way this excercise has been tossed up I am not sure it really would work.

It has been a little while since I have written straight Java but this was fun! Would you change anything? I may ask my buddy to keep forwarding these over for fun.

 
 

Reversing words in a string

A friend was telling me about a question he saw on an exam and wanted to know my thoughts on it. The exam wanted you to write out the function in pseudo code first and then write it in Java and ColdFusion. The question was pretty basic but it was a good one to think about. Write a method that will accept a sentence as an argument. The method will take this string and reverse the order of the words in the argument.

So following our instructions the first thing we need to do is talk out our thought process for this method.

  • create a variable to hold our new string
  • Split the argument into words using a space as the delimiter
  • loop over out list of words
  • take the current word in the loop and prepend it to our new string

That was pretty easy and gives us some really straight forward instructions on how to create our method. First I will write the method in Java. Lucky for us Java has a wonderful class called StringTokenizer. This class will allow us to break our string into tokens and has convenience methods for looping over the tokens and getting the next token.

As you might know ColdFusion can leverage any Java class. Because of that our method in ColdFusion should look pretty much the same. If your not familiar with ColdFusion the init method is just a way for us to pass something into the Java constructor.

 
 

Creating Apache Derby Custom Functions: Part 3

In Part 1 I looked at why I am using Derby and in Part 2 I looked at how to create custom functions. In the final part of this series we are going to look at the actual code to create our functions.

So in the last part we took a look at writing a replace function. In my example I needed to use a replace function on a database field. To create that function we have a class that looks like this. Then I started thinking, what If I need to replace many instances of a string. The function above will only replace the first occurrence. No problem, as I said before we can take this opportunity to create a whole library of functions to use.

When I actually got back to writing my application I noticed another problem. What if I had to replace more than 1 item? With our current replace method I would have to do something like this. That obviously does not make much sense so what I want to do is write a function that allows to replace multiple strings. In my case I am going to write a function that allows for a regular expression. Now I could do something like this. On top of that I came up with a couple other methods that I thought I could use.

I also want to take the chance to get everyone into the mode of unit testing, so please take a chance watch this public service announcement.

As you can see its pretty easy to create your own functions. On top of being extremely easy you can see its pretty powerful. I just really like being able to create methods in a familiar environment. I have attached my jar file that contains my StringUtils class but you can just as easily create your own. I hope to add some more classes + methods in the future so please give me your suggestions.

 
 

More Entries