I read a text file that looks like this:
operationName1 Value
There is a variable number of lines, with different operations and corresponding value. I can read the file and get a 2D String array as the output. This is my code.
try{
Path path = Paths.get("./myFile.txt");
String[][] array = Files.lines(path)
.map(s -> s.split("\\s+", 2))
.map(a -> new String[]{a[0], a[1]})
.toArray(String[][]::new);
}catch(IOException e){
}
Question: How could I change my code to get a 2d int array instead, where operationName1 would be "0" and operationName2 would be "1" (there is only two possible operation, each defined by a specific string)?
This text file:
operationOne 5
OtherOp 999
operationOne 8
operationOne 2
Would become that 2d int array:
[[0,5],[1,999],[0,8],[0,2]]
The index is important too. So the 1st line of the text file is the 1st line of my array.
PS: If there is a better syntax (a more recent one), I am open to suggestion.
Thank you very much.