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