4

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); } } 
1
  • 2
    Have you tried stepping through this with a debugger? What is the length of arggg? What is the parameter for split meant to be? Commented Jul 15, 2015 at 7:56

2 Answers 2

7

. is a special charater in regex. So when using with split() method you need to escape it.

Use,

String[] arggg = tf.split("\\."); 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.