0

I have a Jframe with 3 textFields and a button, when I press the button the program stores the value of the textFields in Strings and checks them against a pattern, here is the relevant part of the code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    Pattern pat = null;
    Matcher mat = null;
    String file = jTextField3.getText();
    pat = Pattern.compile("[a-zA-Z0-9]{1,8}");
    if(file.contains(".")){
         String [] splitFile= file.split(".");
         String fileName = splitFile[0];
         mat = pat.matcher(fileName);
    }
    else{
         mat = pat.matcher(file);
    }
}

I get an ArrayIndexOutOfBounds : 0 on String fileName = splitFile[0], the name of the jTextField is correct and the field is not empty, I tried with 'test.txt' when I got this exception

Thanks for your help

xingbin
  • 27,410
  • 9
  • 53
  • 103
MrCoLLie
  • 11
  • 2

1 Answers1

1

String.split words with regex, while . means any character in regex, you should escape it with two back slashes. One for compiling, one for regex.

String [] splitFile= file.split("\\.");
xingbin
  • 27,410
  • 9
  • 53
  • 103