String Buffer Trim
public class
JavaStringBufferTrimExample
{
public static void
main(String[] args)
{
StringBuffer sbf
= new StringBuffer(" Hello World ! ");
String str = sbf.toString().trim();
System.out.println("StringBuffer
trim: \"" + str +"\"");
System.out.println("\""
+ trim(sbf) + "\"");
}
private static String
trim(StringBuffer sbf)
{
int start, end;
for(start = 0; start
< sbf.length(); start++)
{
if(sbf.charAt(start)
!= ' ')
break;
}
for(end =
sbf.length(); end > start ; end--)
{
if(sbf.charAt(end-1)
!= ' ')
break;
}
return
sbf.substring(start, end);
}
}
Java Convert int Array To String
import java.util.Arrays;
public class ConvertIntArrayToStringExample
{
public static void main(String args[])
{
int[] intNumbers = new int[]{1, 2, 3, 4,5};
StringBuffer sbfNumbers = new StringBuffer();
String strSeparator = " ";
if(intNumbers.length > 0){
sbfNumbers.append(intNumbers[0]);
for(int i=1; i < intNumbers.length;
i++)
{
sbfNumbers.append(strSeparator).append(intNumbers[i]);
}
}
System.out.println("int array converted
to String using for loop");
System.out.println(sbfNumbers.toString());
String strNumbers =
Arrays.toString(intNumbers);
System.out.println("String generated from
Arrays.toString method: " + strNumbers);
strNumbers =
strNumbers.replaceAll(", ", strSeparator).replace("[",
"").replace("]", "");
System.out.println("Final String: "
+ strNumbers);
}
}
Java Reverse String Array
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
public class ReverseStringArrayExample
{
public static void main(String args[])
{
String[] strDays = new
String[]{"Sunday", "Monday", "Tuesday",
"Wednesday"};
List list =
Arrays.asList(strDays);
Collections.reverse(list); strDays =
(String[]) list.toArray();
System.out.println("String array
reversed");
for(int i=0; i < strDays.length; i++)
{
System.out.println(strDays[i]);
}
}
}
String
Buffer To byte Array
public class
StringBufferToByteArrayExample
{
public static void main(String[] args)
{
StringBuffer sbf = new
StringBuffer("Java StringBuffer To byte array example");
byte bytes[] =
sbf.toString().getBytes();
System.out.println("StringBuffer
converted to byte array");
}
}
String Buffer To File
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class JavaStringBufferToFileExample
{
public static void main(String[] args)
throws IOException
{
StringBuffer
sbf = new StringBuffer();
sbf.append("StringBuffer contents first
line.");
sbf.append(System.getProperty("line.separator"));sbf.append("StringBuffer
contents second line.");
BufferedWriter bwr = new BufferedWriter(new
FileWriter(new File("d:/demo.txt")));
bwr.write(sbf.toString());
bwr.flush();
bwr.close();
System.out.println("Content of
StringBuffer written to File.");
}
}
Convert Byte object to String object
public class
ByteToStringExample
{
public static void
main(String[] args)
{
Byte bObj = new
Byte("10");
String str =
bObj.toString();
System.out.println("Byte
converted to String as " + str);
}
}
Display Day of Week using Java Calendar
import java.util.Calendar;
public class DisplayDayOfWeek
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current date : "
+ (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
String[] strDays = new String[]
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thusday",
"Friday",
"Saturday"
};
System.out.println("Current day is :
" +
strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);
}
}
Disable Button
import java.applet.Applet;
import java.awt.Button;
public class DisableButtonExample extends
Applet
{
public void init()
{
Button Button1 = new Button("Ok");
Button Button2 = new
Button("Cancel");
add(Button1);
add(Button2);
Button2.setEnabled(false);
}
}
Java String Buffer setLength
public class JavaStringBufferSetLengthExample
{
public static void main(String[] args)
{
StringBuffer sbf = new
StringBuffer("StringBuffer setLength method example");
sbf.setLength(12);
System.out.println("StringBuffer
contents: " + sbf);
sbf.setLength(0);
System.out.println("StringBuffer contents
deleted:" + sbf);
}
}
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);
}
}
Iterate through elements Java Vector using
ListIterator
import java.util.Vector;
import java.util.ListIterator;
public class
IterateThroughVectorUsingListIteratorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
ListIterator itr = v.listIterator();
System.out.println("Iterating through
Vector elements in forward direction...");
while(itr.hasNext())
System.out.println(itr.next());
System.out.println("Iterating through
Vector elements in backward direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
}
}
Remove All Items From a List
import java.applet.Applet;
import java.awt.List;
public class RemoveAllItemsFromAList extends
Applet
{
public void init()
{
List list = new List(5, true);
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
add(list);
list.removeAll();
}
}
Swap elements of Java ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class SwapElementsOfArrayListExample
{
public
static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before swaping, ArrayList
contains : " arrayList);
Collections.swap(arrayList,0,4);
System.out.println("After swaping,
ArrayList contains : " + arrayList);
}
}
Get Foreground Color Of an Applet Window
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class GetForgroundColorExample extends
Applet
{
public void paint(Graphics g)
{
Color
c = getForeground();
g.drawString(c.toString(),50,50);
}
}
Replace an element at specified index of Java
ArrayList
import
java.util.ArrayList;
public class
ReplaceElementAtSpecifiedIndexArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.set(1,"REPLACED
ELEMENT");
System.out.println("ArrayList
contains...");
for(int index=0; index
< arrayList.size(); index++)
System.out.println(arrayList.get(index));
}
}
Replace an element at specified index of Java
ArrayList
import
java.util.ArrayList;
public class
ReplaceElementAtSpecifiedIndexArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.set(1,"REPLACED
ELEMENT");
System.out.println("ArrayList
contains...");
for(int index=0; index
< arrayList.size(); index++)
System.out.println(arrayList.get(index));
}
}
Create Custom Color Using HSB
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class CreateCustomColorUsingHSB extends
Applet
{
public void paint(Graphics g)
{
Color customColor =
Color.getHSBColor(0.9f,0.5f,0.8f);
this.setForeground(customColor);
g.drawString("This is a custom HSB
color", 10, 50);
}
}
Convert short primitive to Short object
public class shortToShortExample
{
public static void
main(String[] args)
{
short s = 10;
Short sObj = new
Short(s);
System.out.println(sObj);
}
}
Read short from file using DataInputStream
import
java.io.DataInputStream;
import
java.io.FileInputStream;
import java.io.FileNotFoundException;
import
java.io.IOException;
public class
ReadShortFromFile
{
public static void
main(String[] args)
{
String
strFilePath = "C://FileIO//readShort.txt";
try
{
FileInputStream fin =
new FileInputStream(strFilePath);
DataInputStream din =
new DataInputStream(fin);
short s =
din.readShort();
System.out.println("short
: " + s);
din.close();
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException
: " + ioe);
}
}
}
Java String isEmpty
public class
JavaStringIsEmptyExample
{
public static void
main(String args[])
{
String str1 =
"";
String str2 = null;
String str3 =
"Hello World";
System.out.println("Is
String 1 empty? :" + str1.isEmpty()); System.out.println("Is String 3
empty? :" + str3.isEmpty());
}
}
Java float
import java.util.*;
public class
JavaFloatExample
{
public static void
main(String[] args)
{
float = ;
float f = 10.4f;
System.out.println("Value
of float variable f is :" + f);
}
}
Compare Two Numbers
public class
CompareTwoNumbers
{
public static void
main(String[] args)
{
int num1 = 324;
int num2 = 234;
if(num1 > num2)
{
System.out.println(num1
+ " is greater than " + num2);
}
else if(num1 <
num2)
{
System.out.println(num1
+ " is less than " + num2);
}
else
{
System.out.println(num1
+ " is equal to " + num2);
}
}
}
Determine If Year Is Leap Year
public class DetermineLeapYearExample
{
public static
void main(String[] args)
{
int year = 2004;
if((year % 400 == 0) || ((year % 4 == 0)
&& (year % 100 != 0)))
System.out.println("Year
" + year + " is a leap year");
else
System.out.println("Year
" + year + " is not a leap year");
}
}
If Else statement
public class
SimpleIfElseStatementExample
{
public static void
main(String[] args)
{
int i = 0;
if(i == 0)
System.out.println("i
is 0");
else
System.out.println("i
is not 0");
}
}
If Else If statement
public class IfElseIfElseExample
{
public static void main(String[] args)
{
int i = 10;
if(i > 100)
System.out.println("i is grater than
100");
else if(i > 50)
System.out.println("i is grater than
50");
else
System.out.println("i is less than
50");
}
}
No comments:
Post a Comment