Kindly refer to Scanner class. Its having example of, how to use delimiters with Scanner
.
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
Below is the example which is reading your input from a file.
public static void main(String[] args) {
try (Scanner in = new Scanner(new File("D:/INPUT_OUTPUT/Data_Stack.txt"))) {
// Adding delimiters.
in.useDelimiter("\\s*,\\s*|\\s*\\n\\s*|\\s*;\\s*");
// Reading files.
while (in.hasNextDouble()) {
System.out.println(in.nextDouble());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Data in the file (Data_Stack.txt)-:
1, 3
34, 19
0, 14; 6, 19; 9, 15; 7, 8; 1, 9
2, 6; 17, 6; 17, 1; 2, 1
Output-:
1.0
3.0
34.0
19.0
0.0
14.0
6.0
19.0
9.0
15.0
7.0
8.0
1.0
9.0
2.0
6.0
17.0
6.0
17.0
1.0
2.0
1.0
Description-: From Scanner we can see that-:
A Scanner breaks its input into tokens using a delimiter pattern,
which by default matches whitespace.
So we have to add required delimiter to read our data. Required delimiters are ,
, \n
and ;
which have been added in line in.useDelimiter("\\s*,\\s*|\\s*\\n\\s*|\\s*;\\s*");
of code.
After adding delimiters to Scanner
its reading data correctly.