Ho questa stringa che sto prendendo da un file di testo.confuso come .split() funziona in Java
"1 normal 1 [(o, 21) (o, 17) (t, 3)]"
Voglio prendere in 1, normal, 1, o, 21, 17, t, 3
in una matrice di stringhe.
Scanner inFile = new Scanner(new File("input.txt");
String input = inFile.nextLine();
String[] tokens = input.split(" |\\(|\\)|\\[\\(|\\, |\\]| \\(");
for(int i =0 ; i<tokens.length; ++i)
{
System.out.println(tokens[i]);
}
uscita:
1
normal
1
o
21
o
17
t
3
Perché ci sono gli spazi vengano memorizzati nella matrice.