Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, December 7, 2010

Validate textbox in Graphical User Interface containing only numbers


Often it is necessary to validate the input data in the Graphical User Interface when received at the server side. According to the received parameters the server may be not called if the parameters are not in the desired format. This will lead to a saving in the used resources server side. This can be easily done by performing a basic validation on the properties of the object received. The validation proposed here below could be smaller by using regular expressions.
The programming code below just loop on a received string and determines if every character of the string is a digit isDigit(). The loop stops if a character is not a digit and returns false.




/**
*
* @author Jose Ferreiro
*/
public class Main {

/**
* This method checks if a String contains only numbers
*/
public boolean containsOnlyNumbers(String str) {

//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
System.out.println(main.containsOnlyNumbers("123456"));
System.out.println(main.containsOnlyNumbers("123abc456"));
}
}


OUTPUT of the main method:
True
False

HAPPY CODING

Thursday, October 28, 2010

Java lang String Calculate MD5

The java.lang.String class does not provide a faster way to code the MD5 algorithm, surely for security reasons.

Here a quick way to do implement is a a static class

1.import java.security.MessageDigest;
2.import java.security.NoSuchAlgorithmException;
3.
4.public class LMD5Gen {
5.
6. public static String md5(String input){
7. String res = "";
8. try {
9. MessageDigest algorithm = MessageDigest.getInstance("MD5");
10. algorithm.reset();
11. algorithm.update(input.getBytes());
12. byte[] md5 = algorithm.digest();
13. String tmp = "";
14. for (int i = 0; i < md5.length; i++) {
15. tmp = (Integer.toHexString(0xFF & md5[i]));
16. if (tmp.length() == 1) {
17. res += "0" + tmp;
18. } else {
19. res += tmp;
20. }
21. }
22. } catch (NoSuchAlgorithmException ex) {}
23. return res;
24. }
25.
26.}

Friday, March 12, 2010

Logging in Log4j

Locate the log4j.cfg file ...


log4j.rootLogger=INFO, stdout, F
... etc ...
log4j.logger.org.apache.commons=info
log4j.logger.org.apache.axis2=info
log4j.logger.org.apache.axiom=info
#log4j.logger.org.apache.commons.httpclient=debug
#log4j.logger.httpclient.wire=debug
#log4j.logger.httpclient.wire.header=debug


So when you want to see the logging you can remove the "#" as required.

Some relevant links are here ...
"http://hc.apache.org/httpclient-3.x/logging.html"
"http://logging.apache.org/log4j/1.2/manual.html"