0

Im having this really weird issue that i haven't been able to figure out for a few hours. Basically im trying to split this getInterfaceBounds-client.ry, what im doing is this

final String className = line.split(".")[0]; 

im getting a arrayindexoutofbounds exception. I really have no idea why, do you?

Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 0

1
  • Check the size of the array you get when you do line.split(".") Commented Jan 19, 2013 at 5:25

4 Answers 4

3

split uses a regular expression. In regex . means any character, so you need to escape it.

Try:

final String className = line.split("\\.")[0];

See http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum

Sign up to request clarification or add additional context in comments.

Comments

1

The change required is :

final String className = line.split("\\.")[0]; 

Check this example for more details.

  String s="getInterfaceBounds-client.ry";
  String[] arr = s.split("\\.");
  for(String str : arr)
  {
    System.out.println(str);
  }

Ideone link.

Comments

0

Use this instead and it will work. I just tested it.

String line = "getInterfaceBounds-client.ry";

String className = line.split("[.]")[0]; 

System.out.println(className);

The . is a special character in regex which represent any character.

You can learn more about the different special characters in regex here: http://www.fon.hum.uva.nl/praat/manual/Regular_expressions_1__Special_characters.html

Comments

0

Use Array variable for split() beacuse we may get more than a value from splitting so it would be helpful if u use array it would avoid confusion of accesing the values for example:

String line = "getInterfaceBounds-client.ry.test";
String test[] = line.split("[.]");
System.out.println(test[0]+test[1]+test[2]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.