Hi I’m having issues with the code below (file is of the File class):
String[] fileNameSplit = file.getName().split(".");
String fileType = fileNameSplit[(fileNameSplit.length - 1)];
It always throws a ArrayOutOfBoundsExecption at -1 suggesting that fileNameSplit is 0. When I take out the -1 it still says ArrayOutOfBoundsException but now at 0, meaning it is empty.
What am I doing wrong?
This is because
.is a meta-character that accepts “any character”. It treats every single character of your string as a delimiter, “eating up” its entire content.Escape the dot it like this:
or use a character class, like this: