Tuesday, June 5, 2018

String Basic

String to Int

String myString = "1234";
int foo = Integer.parseInt(myString);
Declare Arry

For primitive types:
int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};
For classes, for example String, it's the same:
String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};
The third way of initializing is useful when you declare the array first and then initialize it. The cast is necessary here.
String[] myStringArray;
myStringArray = new String[]{"a","b","c"};
For-each

for (char ch: "xyz".toCharArray()) {
}
List
List<String> someList = new ArrayList<String>();
Get String character by index

String text = "foo";
char a_char = text.charAt(0);
System.out.println(a_char); // Prints f
Character to String

Character.toString("C")


What is the different between int and Integer

http://www.java67.com/2018/02/what-is-difference-between-int-and-integer-in-Java.html

Integer can be null. Bur int cannot be.

int x = null; // This is error

Integer y = null // OK


Only can pass Integer value to the collection class.

List<Integer> element = new ArrayList<>();

we cannot add int value to the element List.

But normally we add value using,
     
       element.add(5);

This work with autoboxing in java, the primitive type int becomes an Integer when necessary.

Autoboxing is the automatic conversion that the java compiler makes between the primitive type and their corresponding wrapper class.

Then 5 convert Integer value by autoboxing.


Friday, June 1, 2018

Convert String to Float

Float.valueOf(getDataByAccountNo.get(i).get("RENTAL").toString())

Check Whether Value is Numeric or String

public static boolean isNumeric(String str) 
    { 
      try 
      { 
        double d = Double.parseDouble(str); 
      } 
      catch(NumberFormatException nfe) 
      { 
        return false; 
      } 
      return true; 
    }

Monday, May 21, 2018

Usage of HttpClient

Add Bellow dependency.




<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.1.1</version>
  </dependency>

--------------------------------------------------------------------------------------------------------------------------




public  String callAPI(String url) throws Exception {
  
  DefaultHttpClient httpClient = new DefaultHttpClient();
  String contentType = null;
  try {
   HttpGet get;
   HttpPost postRequest = new HttpPost(url);
   postRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
   postRequest.addHeader("Accept","application/pdf");
   HttpResponse postResponse = httpClient.execute(postRequest);
   
   int statusCode = postResponse.getStatusLine().getStatusCode();
   
   contentType = postResponse.getFirstHeader("Content-Type").toString();
   
            // Read the contents of an entity and return it as a String.
   //HttpEntity entity = postResponse.getEntity();
            //String content = EntityUtils.toString(entity);
   //int length = content.length();
   if(statusCode != 200){
    throw new RuntimeErrorException(null, "Failed with HTTP error code : " + statusCode);
   }
     
  } finally  {
   httpClient.getConnectionManager().shutdown();
  }
  return contentType;
 } 

Sunday, May 20, 2018

How to create WAR file in Spring Boot

https://stackoverflow.com/questions/47908312/create-war-file-from-springboot-project-in-eclipse

Friday, May 18, 2018

String Class

GetFirstValue

String.valueOf(yourstring.charAt(0))

SubString

yourString.substring(1,10)


Wednesday, May 16, 2018

Annotation - 01 __ @JsonIgnore

The Jackson annotation @JsonIgnore is used to tell Jackson to ignore a certain property (field) of a Java object. The property is ignored both when reading JSON into Java objects, and when writing Java objects into JSON. Here is an example class that uses the @JsonIgnore annotation:


import com.fasterxml.jackson.annotation.JsonIgnore;

public class PersonIgnore {

    @JsonIgnore
    public long    personId = 0;

    public String  name = null;
}



In the above class the property personId will not be read from JSON or written to JSON.


Source By - http://tutorials.jenkov.com/java-json/jackson-annotations.html#jsonignore

CoreJava 0n Java finally block


Java Finally block is a block that is used to execute important code such as closing connection, stream etc.

Java Finally block is always executed whether exception handles or not.

Finally, block in java can be used to put "cleanup" code such as closing a file, closing connection etc.

For each try block, there can be zero or more catch blocks, but only one finally block

The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).                                                      Ref - https://teck4world.blogspot.com/2018/05/systemexitint-x.html


Case 01 - The exception doesn't occur.


  1. class TestFinallyBlock{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int data=25/5;  
  5.    System.out.println(data);  
  6.   }  
  7.   catch(NullPointerException e){System.out.println(e);}  
  8.   finally{System.out.println("finally block is always executed");}  
  9.   System.out.println("rest of the code...");  
  10.   }  
  11. }  



Case 02 - The exception occurs and not handled.


  1. class TestFinallyBlock1{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int data=25/0;  
  5.    System.out.println(data);  
  6.   }  
  7.   catch(NullPointerException e){System.out.println(e);}  
  8.   finally{System.out.println("finally block is always executed");}  
  9.   System.out.println("rest of the code...");  
  10.   }  
  11.  
Case 03 - The exception occurs and handled.

  1. public class TestFinallyBlock2{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int data=25/0;  
  5.    System.out.println(data);  
  6.   }  
  7.   catch(ArithmeticException e){System.out.println(e);}  
  8.   finally{System.out.println("finally block is always executed");}  
  9.   System.out.println("rest of the code...");  
  10.   }  
  11. }