Friday, August 19, 2011

Some advice

I stumbled upon this while reading a blog.

- An hour of sleep before midnight is worth two, and an hour of work before noon is worth two.

- Always pick your kids up from school. That’s when they want to talk.

- Never let your skill exceed your virtue.

- Never take less than two weeks off when you have a child or for your honeymoon. Don’t let them talk you down.

- When you mess up, admit it frankly and quickly, and move on.

- Always do your very best in your job, but if you don’t like what you’re doing enough that you would do it for free, quit.

Friday, July 15, 2011

MYSQL: Change mysql db schema name

How to change mysql database schema name?

Here s what you need to do:
1. Use mysqldump to dump old mysql database.
2. Create new database using mysqladmin.
3. And run the sql generated via mysqldump.

e.g. if you want to change database named "school_old" to "school_new"
Here are the commands.
mysqldump -u username -p -v school_old > old_db_dump.sql
mysqladmin -u username -p create school_new
mysql -u username -p school_new < old_db_dump.sql

Wednesday, June 15, 2011

Updated twitter bookmarklet

Recently twitter updated the way you post tweets. So the previous version of bookmarklet stopped working.

So here is the updated version. Just drag this bookmarklet to your bookmarks bar.

Twitter Bookmarklet

Check out his link if you want to know what this bookmarklet does.
Click here

Here is the code:
javascript: var longUrl=document.location; var url = 'http://api.bit.ly/shorten?version=2.0.1&login=tweetthees&apiKey=R_e1266a2cb3177ea0a9e1114cc49d8ad9&longUrl='+longUrl; loadScript(url+'&callback=tweetme'); function loadScript(scriptURL){ var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language','JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem); } function tweetme(json){ var shortLink=json.results[longUrl].shortUrl; window.location='http://twitter.com/intent/tweet?text=Reading: '+document.title+' '+shortLink }

Tuesday, April 12, 2011

Tuesday, July 6, 2010

JAVA: Convert String to InputStream

You can use ByteArrayInputStream to convert String to InputStream.
ByteArrayInputStream extends InputStream.

Example:
String content = "Convert string to inputstream";
ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes());



Reference:
1. http://forums.sun.com/thread.jspa?threadID=670188

2. http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream.html

Wednesday, May 12, 2010

JSTL: Find length of a collection

To get a length of a collection:
${fn:length(tvPackageGroup.addonOptions)}

Include <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> namespace.
http://stackoverflow.com/questions/851880/check-a-collection-size-with-jstl

Sorting a collection in JSTL

javadoc: http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

Tuesday, February 2, 2010

JAVA: reverse string interview question

When you go for an interview, lot of companies ask String manipulation questions.


Some of the most common questions are:

  • Reverse words in a string (words are separated by spaces) - Most Common
  • Reverse String
  • Revers the order of words

I have written a code that performs all these functions. Hope this will help you.

/**
* Created by IntelliJ IDEA.
* User: priyank
* Date: Feb 2, 2010
*/
public class StringTest {

public static void main(String[] args) {
String str = "My name is khan";
// String result = reverseString(str);
// String result = reverseWords(str);
// String result = reverseSequence(str);
String result = iReverseSequence(str);

System.out.println(result);
}

static String reverseString(String str) {
char[] ch = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i=(ch.length-1);i>=0;i--) {
sb.append(ch[i]);
}
return sb.toString();
}

static String reverseWords(String str) {
String[] st = str.split("\\s");
StringBuilder sb = new StringBuilder();
for(int i=0; i < st.length;i++) {
sb.append(reverseString(st[i])+" ");
}
return sb.toString();
}

static String reverseSequence(String str) {
String[] st = str.split("\\s");
StringBuilder sb = new StringBuilder();
for(int i=(st.length-1);i>=0;i--) {
sb.append(st[i]+" ");
}
return sb.toString();
}


// Without using inbuilt java functions (for ex without using split())
// I still used substr() though
static String iReverseSequence(String str) {
String word = "";
int lastIndex = str.length();
for(int i=(str.length()-1);i>=0;i--) {
if(str.charAt(i) == ' ') {
word += str.substring(i+1,lastIndex)+" ";
lastIndex = i;
}
}
word += str.substring(0,lastIndex);
return word;
}
}