How to capitalize first char in a String Java

Capitalize first char

In this post I would like to show you how to put the first letter into upper case in Java:

  1. Example:
    This is the method to do it:

    private String capitalizeFirstLetter(final String line) {
       String result="";
       if( result != null){
            result = Character.toUpperCase(line.charAt(0)) + line.substring(1);
       return result;

Reference link:

How to round a Double value in Java

With this code, you can round a double in Java

  1. Using DecimalFormat method:
    double d = 1.234567;
    DecimalFormat df = new DecimalFormat("#.##");
  2. Using BigDecimal method:
    public static void main(String[] args)
          double value = 1254.625;
          String val = value+"";
          BigDecimal big = new BigDecimal(val);
          big = big.setScale(2, RoundingMode.HALF_UP);
          System.out.println("Number : "+big);

Reference link:

Encode and Decode JSON text from Java to JavaScript – Using List

JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.

  1. Mapping between JSON and JAVA entities: 
    JSON Java
    string java.lang.String
    number java.lang.Number
    true|false java.lang.Boolean
    null null
    array java.util.List
    object java.util.Map
  2. Example:
    //import org.json.simple.JSONObject;
    Encode a java.util.List in Java

      JSONObject obj = new JSONObject();
      obj.put("num",new Integer(100));
      obj.put("balance",new Double(1000.21));
      obj.put("is_vip",new Boolean(true));
      String jsonText = JSONValue.toJSONString(obj);

    Decode it in JavaScript using JSONObject


      var js_obj = JQuery.parseJSON(jsonText);
      var name = js_obj[0].name;
      var num = js_obj[0].num;

Reference Link:
Reference Link:
Reference Link:

Java – List Example

List example give a method, how to use List in java:

  1. Code:
    import java.util.Iterator;
    import java.util.List;
    import java.util.ArrayList;
    public class ListExample {
        public static void main(String[] args) {
            // List Example implement with ArrayList
            List<String> ls=new ArrayList<String>();
            Iterator it=ls.iterator();
              String value=(String);
              System.out.println("Value :"+value);
  2. Output:
    List Value :One
    List Value :Three
    List Value :two
    List Value :four

Reference Link:

How to convert string to (double, int or boolean) and int to String in Java

With this code, you can convert string to double or to int or to boolean and convert int to string in Java:

  1. Convert string to int:
    String aString = "78";
    int aInt = Integer.parseInt(aString);
  2. Convert string to double:
    String aString = "78";
    double aDouble = Double.parseDouble(aString);
  3. Convert string to boolean:
    String aString = "true";
    double aBoolean = Boolean.valueOf(aString);
  4. Convert int to string:
    int aInt = 1;    
    String aString = Integer.toString(aInt);
  5. Convert double to string:
    double aDouble = 1.0;    
    String aString = Double.toString(aDouble);