Saturday, November 1, 2014

Java Better Pro



Parse CSV File using String To kenizer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class ParseCSVFileExample
{
 public static void main(String[] args)
{
 try
{
String strFile = "C:/FileIO/example.csv";
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (strLine = br.readLine()) != null)
{
lineNumber++;
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens())
{
tokenNumber++;
System.out.println("Line # " + lineNumber +
", Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
tokenNumber = 0;
  }
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);                  
      }
    }
}

Reverse string by word using String To kenizer

import java.util.StringTokenizer;
public class ReverseLine
{
 public static void main(String[] args)
{
String strLine = "Java Reverse string by word example";
StringTokenizer st = new StringTokenizer(strLine, " ");
String strReversedLine = "";
while(st.hasMoreTokens())
{
strReversedLine = st.nextToken() + " " + strReversedLine;
}
System.out.println("Reversed string by word is : " + strReversedLine);
   }
}

Find Square Root of Big Integer

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
public class SquareRootOfBigIntegerExample
{
public static void main(String[] args)
{
SquareRootOfBigIntegerExample SquareRootOfBigIntegerExample = new SquareRootOfBigIntegerExample();
String n = "";
MathContext mc =  new MathContext(0, RoundingMode.DOWN);
mc = MathContext.DECIMAL32;
                  BigInteger my2P100000  = new BigInteger("0");
                  BigInteger two      = new BigInteger("2");
                  BigInteger one      = new BigInteger("1");
my2P100000  = two.shiftLeft(2000 - 1);
System.out.println("2^2000 --  Step 1");
System.out.println("Value of 2^2,000 " + my2P100000  );
System.out.println("");
System.out.println("Finding the Square Root of 2^2000");
String mys =  my2P100000  + "";
n = (mys) ;
int firsttime = 0;
BigDecimal myNumber = new BigDecimal(n);
BigDecimal g = new BigDecimal("1");
BigDecimal my2 = new BigDecimal("2");
BigDecimal epsilon = new BigDecimal("0.0000000001");
        
BigDecimal nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);
BigDecimal nBygPlusg = nByg.add(g);
               
BigDecimal nBygPlusgHalf =  nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);
BigDecimal  saveg = nBygPlusgHalf;
firsttime = 99;
do
{
g = nBygPlusgHalf;
nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);
nBygPlusg = nByg.add(g);
nBygPlusgHalf =  nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);
BigDecimal  savegdiff  = saveg.subtract(nBygPlusgHalf);
 if (savegdiff.compareTo(epsilon) == -1 )
{
firsttime = 0;
}
else
{
saveg = nBygPlusgHalf;
}
}
 while (firsttime > 1);
System.out.println("For " + mys + "\nLength: " + mys.length() + "\nThe Square Root is " + saveg);
        }
}

No comments:

Post a Comment