private String removeExtraSpaces(String str){
        int i,j;
        StringBuilder sb = new StringBuilder(str);       
        for(i = 0; i < sb.length(); i++){
            if(Character.isSpaceChar(sb.charAt(i))){
                j = i + 1;
                while(j < sb.length() && Character.isSpaceChar(sb.charAt(j))){
                    sb.deleteCharAt(j);
                }
            }
        }
        if(sb.length() > 0)
            if(Character.isSpaceChar(sb.charAt(sb.length() - 1)))
                sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }