Monday, November 7, 2011

close file handler in java while operating on files

I just bumped into one of the error with file io. if you dont close fie handler, it wont write anything into file.
import java.io.*;
class FileWrite 
{
 public static void main(String args[])
  {
  try{
  // Create file 
  FileWriter fstream = new FileWriter("out.txt");
  BufferedWriter out = new BufferedWriter(fstream);
  out.write("Hello Java");
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

executing external process using java

Runtime rt=Runtime.getRuntime();
  Process x= rt.exec("ls -l");
  BufferedReader stdInput = new BufferedReader(new 
                InputStreamReader(x.getInputStream()));
  String s;
  while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

Adding new elements to arraylist in java while using iterator

today i had a problem with usage of arraylist along side of iterator. the problem was that i was unable to add elements to the arraylist. it gives you an error java.util.ConcurrentModificationException this is because u cant add elements to an arraylist while using an iterator. so i had used listiterator as below.

import java.util.ArrayList;
import java.util.ListIterator;
public class Test 
{
 public static void main(String args[])
 {
  ArrayList array_test= new ArrayList();
  array_test.add("a");
  array_test.add("k");
  array_test.add("d");
  array_test.add("s");
  array_test.remove("d");
  int i=1;
  for(ListIterator it=array_test.listIterator();it.hasNext();)
  { 
   String link=it.next(); 
   if(i==1)
   {  
    it.add("r");
    it.previous();
    it.add("kk");
    it.previous();
   i++;
   }
   System.out.println(link);
   
  } 
   
  System.out.println("Contents of arrays list "+array_test);
 }
 

}

In the above code i had used it.previous whenever i used it.add. this is because i want to use the added value in the loop again. while the addition of new element in array is added just before next element in arraylist.

Thursday, October 6, 2011

perl object oriented tutorial
http://www.tutorialspoint.com/perl/perl_oo_perl.htm

Wednesday, September 28, 2011

collections in java
array list algorithm (dynamic array)
map
hashmap ( unordered data)
linkedhashmap ( ordered data)
- treemap are type of linked hash map better in implementation
java iterators for getting next element link
mutable and imutable onbjects link

sql query to find data between an interval.. something like getting data for each week

SELECT MAX( STR_TO_DATE( CONVERT( `PurchaseDt`

USING utf8 ) , '%m-%d-%Y' ) ) AS maxdate, MIN( STR_TO_DATE( CONVERT( `PurchaseDt`

USING utf8 ) , '%m-%d-%Y' ) ) AS mindate

FROM `MemberEarnings`

WHERE STR_TO_DATE( CONVERT( `PurchaseDt`

USING utf8 ) , '%m-%d-%Y' )

BETWEEN STR_TO_DATE( '08-01-2011', '%m-%d-%Y' )

AND STR_TO_DATE( '08-21-2011', '%m-%d-%Y' )

GROUP BY DATE_SUB( STR_TO_DATE( CONVERT( `PurchaseDt`

USING utf8 ) , '%m-%d-%Y' ) , INTERVAL DAYOFWEEK( STR_TO_DATE( CONVERT( `PurchaseDt`

USING utf8 ) , '%m-%d-%Y' ) ) -1

DAY )

Thursday, September 15, 2011

scala (new programming language)

data types

def to define objects
val final variables
var which can be changed...

peano Axioms

http://jim-mcbeath.blogspot.com/2008/11/practical-church-numerals-in-scala.html

99 scala problems