Why this code does not work?
String s = "0.1";
String[] sa = s.split(".");
System.out.println(sa[0] + "Hello " + sa[1]);
It gives the error as :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.test.A.main(A.java:8)
String.split doesn’t split the string by another string, it splits it by a regular expression. The
.has a special meaning in regular expression (it stands for “any character”). So when you want to explicitely match dots, you need to escape them. Use"\\."instead.