Wednesday, November 5, 2014

JAVA IN ARRAY TO PROGRAM



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]);
   
     }
  }

No comments:

Post a Comment