Can someone please help me find the problem with the following code: it keeps giving me an:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
code
public class Hello{
public static void main(String[] args){
String tf = "192.168.40.1";
String[] arggg = tf.split(".");
String add = arggg[0];
System.out.println(add);
}
}
.
is a special charater in regex. So when using with split()
method you need to escape it.
Use,
String[] arggg = tf.split("\\.");
So here is the case. Dot is the Reguler Expression and when using Reguler expression with spilt()
method it splitting using Reguler expression. You can get more detailed idea about it by following http://www.regular-expressions.info/dot.html link.
What you need to do is use escape charactor and tell split method you need to split using "."
String[] arggg = tf.split("\\.");
will solve your issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With