check if string is palindrome in java using string buffer

package com.codefloor;
import java.util.*;
public class palindrome {
 public static void main(String[] args) {
 Scanner s=new Scanner(System.in);
 System.out.println("Enter the string to check for palindrome");
 String st1=s.nextLine();
 StringBuffer sb=new StringBuffer(st1);
 sb.reverse();
 if(st1.equals(sb.toString()))
 System.out.println("It is a Palindrome String");
 else
 System.out.println("Not a Palindrome String");
 }
}

Getting classpath from the class is loaded in java

package com.codefloor;

import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class getClassPath{
public static void main(String[] args) {
try{
Class clsObj = Class.forName("com.codefloor.arrayList");
ProtectionDomain proDomain = clsObj.getProtectionDomain();
CodeSource codeSrc = proDomain.getCodeSource();
URL url = codeSrc.getLocation();
System.out.println("actual class path is " + url.getFile());
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Output

actual class path is /C:/codefloor/working/com.codefloor/bin/

Creating Array List and Sorting it in Java

package com.codefloor; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class arrayList{ public static void main(String args[]){ List<String> nameList = new ArrayList<String>(); nameList.add(“Johny”); nameList.add(“Henry”); nameList.add(“Mark”); nameList.add(“Britto”); System.out.println(“Names in unsorted order”); for(String name: nameList){ System.out.println(name); } Collections.sort(nameList); System.out.println(“Names in sorted order”); for(String name: nameList){ System.out.println(name); } } } Output Names in unsorted order Johny Henry Mark Britto…

Details

Creating XML file using JAXB in Java

package com.codefloor; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement class xmlObject { String EmpName; int EmpAge; int EmpId; public String getEmpName() { return EmpName; } @XmlElement public void setEmpName(String EmpName) { this.EmpName = EmpName; } public int getEmpAge() { return EmpAge; } @XmlElement public void setEmpAge(int EmpAge) { this.EmpAge = EmpAge;…

Details

Reading a file in java through FileInputStream

package com.codefloor; import java.io.File; import java.io.FileInputStream; public class InputStreamExample { public static void main(String[] args) { File file = new File(“C:/samplefile.txt”); FileInputStream fis = null; try { fis = new FileInputStream(file); int data; System.out.print(“File Content is :”); while ((data = fis.read()) != -1) { System.out.print((char) data); } } catch (Exception e) { e.printStackTrace(); } finally…

Details

How to create Enum in java

An enum type is a special data type that enables predefined constants for a variable. package com.codefloor; enum Empl { Johny(22),Jackson(20),Mark(25),Tony(19),Gilbert(30); private int age; Empl(int p) { age = p; } int getAge() { return age; } } public class enumEmpl { public static void main(String args[]){ System.out.println(“All Employee Age:”); for (Empl e : Empl.values())…

Details

Reading CSV file in java

sample.csv 1,johnny,Sales 2,Samy,Manager 3,Michel,Analyst package com.codefloor.csv; import java.io.BufferedReader; import java.io.FileReader; public class readingCSV { public static void main(String[] args) { BufferedReader br = null; String singleLine = “”; try { br = new BufferedReader(new FileReader(“sample.csv”)); while ((singleLine = br.readLine()) != null) { String[] data = singleLine.split(“,”); // Splitted by , System.out.println(“Employee – name= ” +…

Details

Executing a Oracle Procedure in Java

package com.codefloor; import java.sql.CallableStatement; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; public class executeProcedure { public static void main(String[] argv) { try { callOracleProc(); } catch (SQLException e) { System.out.println(e.getMessage()); } } private static void callOracleProc() throws SQLException { Connection dbConnection = null; CallableStatement callableStatement = null; String insertStoreProc = “{call sampleProc(?,?,?)}”; //Procedure should have only…

Details