Tuesday, October 20, 2009

JSP - insert simple data with jsp expression

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>java expression example</title> 
    </head> 
    <body> 
        <h1>A JSP TimeDate Stamp Example</h1> 
<!-- 
 you can use jsp expressions with the 
 shortcut percent equals or you can use 
 jsp:expression tags 
 
 --> 
 
        <p> Here is the date using the shortcut
        percent equals syntax:<br /> 
        <%= new java.util.Date() %> </p> 
 
        <p> Here is the same date stamp using the
        jsp expression tags: <br /> 
        <jsp:expression>new java.util.Date()</jsp:expression></p> 
 
 
    </body> 
</html> 
<!-- Here is my output: 
 
A JSP TimeDate Stamp Example 
 
Here is the date using the shortcut percent equals syntax: 
Tue Oct 20 10:52:53 MDT 2009 
 
Here is the same date stamp using the jsp expression tags: 
Tue Oct 20 10:52:53 MDT 2009 
 
--> 
 

Tuesday, October 6, 2009

Java - pig latin generator

 
package javasomethingsomething;
 
public class Main {
    public static void main(String[] args) {
        // all the heavy lifting is done in the PigLatin class 
        PigLatin pg = new PigLatin();
        String pigSentence = "";
        String sentence = "This is going to be a pig latin sentence";
        // iterate over the sentence with for-each and split by " " 
        for (String s:sentence.split(" ")) {
            pigSentence += pg.getPigLatinForWord(s) + " ";
        }
        // added the trim to remove the trailing space 
        System.out.println(pigSentence.trim());
    }
 
}
 
class PigLatin{
 
    public String getPigLatinForWord(String word) {
        int firstVowel = word.length();
        // yes, I included y as a vowel!! 
        String[] vowels = {"a", "e", "i", "o", "u", "y"};
        for(String s:vowels){
            // if indexOf() finds no match it returns -1 
            if ((word.indexOf(s) < firstVowel) && (word.indexOf(s) != -1)) {
                firstVowel = word.indexOf(s);
            }
        }
        return  word.substring(firstVowel, word.length()) +
                word.substring(0, firstVowel) +
                "ay";
    }
}
 

Friday, September 25, 2009

Java - a ping example with java's InetAddress.isReachable()

import java.net.*;
public class Main {
 
    public static void main(String[] args) {
        try{
            // a note: 
            // This the isReachable method is problematic 
            // If you are in a coorporate environment 
            // then you are probably (unknown to you) 
            // using proxy servers.  Java needs to know 
            // what the proxy server and port is 
            // DhttpProxy.host=yourproxyhost.com -DhttpProxy.port 
            // 
            // All of testing I've done at home works fine. 
            // You need to configure java to utilize your proxy 
 
            // gather the ip address associated with the host 
            InetAddress[] addresses = InetAddress.getAllByName("yahoo.com");
 
            // iterate through the ip address with for-each 
            for (InetAddress addr:addresses) {
 
                // the timeout is in milliseconds 2 seconds here 
                if (addr.isReachable(2000)){
                    System.out.printf("%s is reachable", addr);
                    System.out.println();
                }
                else{
                    System.out.printf("%s is not reachable", addr);
                    System.out.println();
                }
            }
        }
        catch(Exception e) {
            System.out.println("host is unknown (or unresolvable)");
        }
    }
}
/* 
 *      Output (for me): 
        yahoo.com/69.147.114.224 is reachable 
        yahoo.com/209.131.36.159 is reachable 
        yahoo.com/209.191.93.53 is reachable 
*/ 
 

Java - reverse the word order of a sentence

public class Main {
 
    public static void main(String[] args) {
        Words w = new Words();
        String sentence = "The example sentence is here";
        System.out.println(sentence);
        System.out.println("reversed to:");
        System.out.println(w.reverseWordOrderOfSentence(sentence));
    }
}
 
class Words{
    public String reverseWordOrderOfSentence(String sentence){
        // split up by words 
        String[] words = sentence.split(" ");
        sentence = "";
 
        // iterate backward through the array 
        // rebuilding the sentence from the back forward 
        for (int i=words.length; i>0; i--){
            sentence += words[i-1];
            sentence += " ";
        }
        // remove the trailing white space 
        return sentence.trim();
    }
}
 
/* 
 *      OUTPUT: 
        The example sentence is here 
        reversed to: 
        here is sentence example The 
*/ 
 

Java - extracting rgb values from an image

import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) {
        BufferedImage bimg = null;
        String input = "test.jpg";
        try{
            bimg = ImageIO.read(new File(input));
        }
        catch (Exception e) {
            // the test.jpg must be in the working dir  
            System.out.println(input + " is not in working dir");
        }
 
        System.out.println("height: " + Integer.toString(bimg.getHeight()));
        System.out.println("width: " + Integer.toString(bimg.getWidth()));
 
        // collect the rgb information 
        // regarding a single pixel 
        int rgb = bimg.getRGB(10, 10);
 
        // for some smart reason (that is beyond me) 
        // getting the rgb gives you a single int 
        // 
        // each component of colr occupies 8 bits 
        // this extracts the actual alpha, red, green, blue 
        int a = (rgb >>> 24) & 0xFF;
        int r = (rgb >>> 16) & 0xFF;
        int g = (rgb >>> 8) & 0xFF;
        int b = (rgb >>> 0) & 0xFF;
 
        // here is the proof 
        System.out.println("a: " + Integer.toString(a));
        System.out.println("r: " + Integer.toString(r));
        System.out.println("g: " + Integer.toString(g));
        System.out.println("b: " + Integer.toString(b));
 
        // this scripts output: 
        /* 
            height: 564 
            width: 634 
            a: 255 
            r: 222 
            g: 128 
            b: 184 
        */ 
    }
 
}
 
 

Java - remove vowels from a sentence String

public class Main {

    public static void main(String[] args) {
      NotStatic g = new NotStatic();
      String sentence = "This is my example sentence.";
      String s = g.RemoveVowelsFromSentence(sentence);
      System.out.println(s);

      // output:

      //    Ths s m xmpl sntnc.

    }
}
class NotStatic {

    public String RemoveVowelsFromSentence(String sentence) {
        String[] vowels = {"a", "e", "i", "o", "u", "y"};
        // iterate through the vowel array with for-each

        for (String s:vowels){
            sentence = sentence.replaceAll(s, "");
        }

        return sentence;

    }

}


Java - Hashtable example - how to create and use

import java.util.*;

public class Main {

    public static void main(String[] args) {
        // a hashtable is like a dictionary

        // to put items into the hashtable you 
        // need a key and a value.  The keys must
        // be unique -- while the values can be
        // anything you like
        Hashtable ht = new Hashtable();

        // put new values into ht

        ht.put("name", "Steve");
        ht.put("warpFactor", 9);

        String s = "";
        // looping add
        for (int i=0; i<20; i++) {
            s = Integer.toString(i);
            s += " is a good number";
            ht.put(i, s);
        }

        // use the values in the hashtable

        System.out.println("I hear that " +
                ht.get("name").toString() +
                " has a Warp Factor of " +
                ht.get("warpFactor").toString());

        // outputs:
        // I hear that Steve has a Warp Factor of 9

    }
}