Monday, October 27, 2014

CONVERT PROGRAMES

Convert String to long
public class StringToPrimitiveLongExample
{
public static void main(String[] args)
{
String str = new String("10");
long l = Long.parseLong(str);
System.out.println(l);
   }
}
Get Selected Item From Single Select AWT List
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class GetSelectedItemFromSingleSelectExample extends Applet implements ItemListener
{  
List list = null;
public void init()
{
list = new List(5, false);
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
add(list);
list.addItemListener(this);
}
public void paint(Graphics g)
{
g.drawString("Selected Item: "+ list.getSelectedItem(), 10, 120);
}
public void itemStateChanged(ItemEvent arg0)
                    {
repaint();     
  }
}
Performing Binary Search on Java float Array
import java.util.Arrays;
public class BinarySearchFloatArrayExample
{
public static void main(String[] args)
{
float floatArray[] = {1.23f,2.10f,4.74f,5.34f};
Arrays.sort(floatArray);float searchValue = 4.74f;
int intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 4.74 is : " + intResult);
searchValue = 3.33f;
intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 3.33 is : " + intResult);
   }
}
Get Synchronized List from Java ArrayList
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GetSynchronizedListFromArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList(arrayList);  
   }
}

Read file using FileInputStream
import java.io.*;
public class ReadStringFromFile
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadString.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try 
 { 
 fin = new FileInputStream(file);
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();
}
catch(FileNotFoundException e)
{
System.out.println("File " + file.getAbsolutePath() +
" could not be found on filesystem");
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file" + ioe);
}
System.out.println("File contents :");
System.out.println(strContent);
  }

}

No comments:

Post a Comment