I have this format of data

```
n
l
p1(x1) p1(x2) imp(p1)
p2(x1) p2(x2) imp(p2)
: : :
pl(x1) pl(x2) imp(pl)
r
q1(x1) q1(x2) imp(q1)
q2(x1) q2(x2) imp(q2)
: : :
qr(x1) qr(x2) imp(qr)
```

where n, l(number of p’s) , and r(number of q’s) are integers. I know how to read only integers from a file but how I can read the lines that includes strings and get the values of x1, x2, and p1 for each line?! Thanks in Advance.

Here is my code for reading integers only.

```
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
// The first line gives the number of nodes (You will use to create the int[][] graph = new int[nOfNodes][nOfNodes];)
if (c == 0) {
numberOfNodes = Integer.parseInt(text.trim());
} // The second one gives the number of edges
else if (c == 1) {
nOfEdges = Integer.parseInt(text.trim());
graph2 = new double[nOfEdges][3];
} // And third the list of special nodes
// `nodes` will now contains only your special constrained one
else if (c == 2) {
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
nodes.add(Integer.parseInt(str[i]));
}
}
} else { // Then you have your edges descriptors
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
graph2[c - 4][i] = Double.parseDouble(str[i]);
}
}
}
c++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.print(e);
}
}
```

**Solution:**

Using matcher class like this

```
public static int numberOfPoints = -1, p=-1, q=-1;
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Integer> dots = new ArrayList<>();
int c = 0;
File file = new File("D:\\ALGORTHIMS\\MASTER LEVEL\\dr. khaled\\assignment 3\\a.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
try {
if(c==0)
{ numberOfPoints=Integer.parseInt(text); c=1;}
if(c==1)
{ p=Integer.parseInt(text);c=2;}
if(c==2)
q=Integer.parseInt(text);
} catch (NumberFormatException e) {
Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(text);
while (m.find()) {
dots.add(Integer.parseInt(m.group(1)));
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.print(e);
}
}
```