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

0

1 Answer 1

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("\\.");
Sign up to request clarification or add additional context in comments.

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.