-2

sorry if the question has been already asked, i just got a "java.lang.StringIndexOutOfBoundsException" thrown though my program was running correctly at the first try. I have tried to change the index of my substring but it's not working either. here's the full exception thrown:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 8, end 17, length 9
at java.base/java.lang.String.checkBoundsBeginEnd(Unknown Source)
at java.base/java.lang.String.substring(Unknown Source)
at platerecognition.PlateRecognition.main(PlateRecognition.java:31)

and here's the lines:

public class Asserv {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {

   final File folder = new File("./Verr");

   PrintWriter pw = new PrintWriter(new File("Gel.csv"));

   for (final File fileEntry : folder.listFiles()) {
    if (!fileEntry.isDirectory()) { 
        String filename = fileEntry.getName();
        String date = filename.substring(0, 8);
        System.out.println(date);
        String time = filename.substring(8,17);
        System.out.println(time);
        int index = filename.indexOf("_", 19);
        String plate = filename.substring(18,index);
        System.out.println(plate);        
        int index2 = filename.indexOf("-", index+2);
        String cam = filename.substring(index+4, index2);
        System.out.println(cam);
        String last = filename.substring(index2 + 1, filename.indexOf('.', index2 + 1));
        System.out.println(last);
        System.out.println(fileEntry.getName());
Yacine Walid
  • 61
  • 2
  • 9

1 Answers1

2

You can read the exception message begin 8, end 17, length 9 like the following :

You asked for a substring from 8(begin) to 17(end) but the current length is 9.

From String.substring

Throws:

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

You need to check the length of the String to prevent this exception. Something like :

int begin = 8;
int end = 17;
s.substring(begin, Math.min(s.length(), end));

I used Math.min(s.length(), end) to get the lowest value, it will be either the end or the limit of this String.

Note: This would be the same problem if begin is larger than end (or the length). So this is not fully safe but you get the idea.

Here is a quick method

public static String substring(String s, int begin, int end){
    //Prevent out of bounds by stopping at the end of the `String`
    end = Math.min(end, s.length());

    return s.substring(begin, end);
}
Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • the substring from 8 to 17 has exactly a length of 9 so i don't think that it goes too far – Yacine Walid Apr 16 '18 at 10:31
  • I believe `length` is the length of the `String` on which you call `substring`, not the length of the substring itself. I have to guess since this message depends on the JDK used. But the documentation from `String.substring` is quite explicit. – AxelH Apr 16 '18 at 10:33
  • the length of the string from which i call the substring is 20, which is not too far also, what i don't understand too is that the program worked correctly at first try – Yacine Walid Apr 16 '18 at 10:37
  • What do you mean with "_first try_" @YacineWalid ? – AxelH Apr 16 '18 at 10:39
  • I mean the first time i made the run – Yacine Walid Apr 16 '18 at 11:42
  • Provide a [mcve] @YacineWalid, this include the data on which the process throw the exception. The explanation I gave is the only one i see. I don't understand what you mean by "_the first time i made the run_" since I don't know what you have ran exactly. – AxelH Apr 16 '18 at 12:26
  • okey done ! you can check and tell me if you need more – Yacine Walid Apr 16 '18 at 12:43
  • Can i ask also about why i get the (Unknown Source) error ? – Yacine Walid Apr 16 '18 at 12:56
  • Hello everyone ! the source of the problem was the JDK version as i was working first on 1.7 (oldest) and i updated to 9.0 ! Thank you all for your answers – Yacine Walid Apr 17 '18 at 08:28
  • @YacineWalid. What unknown source .... be specific, we are not in your mind. About the "solution", `substring` documentation didn't changed in Java 9 so you will get an exception too. I would guess this was your `File` that was incorrect but since you never provide a [mcve] I can't confirm. All I can say is that `substring` in java 7 work the same as Java 8 and from what I see Java 9. – AxelH Apr 17 '18 at 08:31
  • Hello @AxelH, i am really sorry, i understand what you asking me but i couldn't answer you in the proper way, maybe if you have time we can start a private chat and get in deep of the problem, anyway, since i updated to JDK 9.0 no exception is thrown and the program is running correctly with a coverage of 99% – Yacine Walid Apr 17 '18 at 08:57