I am using split()
to tokenize a String separated with *
following this format:
name*lastName*ID*school*age % name*lastName*ID*school*age % name*lastName*ID*school*age
I'm reading this from a file named "entrada.al" using this code:
static void leer() { try { String ruta="entrada.al"; File myFile = new File (ruta); FileReader fileReader = new FileReader(myFile); BufferedReader reader = new BufferedReader(fileReader); String line = null; while ((line=reader.readLine())!=null){ if (!(line.equals("%"))){ String [] separado = line.split("*"); //SPLIT CALL names.add(separado[0]); lastNames.add(separado[1]); ids.add(separado[2]); ages.add(separado[3]); } } reader.close(); }
And I'm getting this exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *
My guess is that the lack of a *
after age on the original text file is causing this. How do I get around it?
No, the problem is that *
is a reserved character in regexes, so you need to escape it.
String [] separado = line.split("\\*");
*
means "zero or more of the previous expression" (see the Pattern
Javadocs), and you weren't giving it any previous expression, making your split expression illegal. This is why the error was a PatternSyntaxException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With