我有这种格式的数据
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)
其中n,l(p的数量)和r(q的数量)是整数。我知道如何从文件中只读取整数,但是如何读取包含字符串的行并获取每行的x1,x2和p1的值?!提前致谢。
这是我的读取整数的代码。
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);
}
}
像这样使用matcher类
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);
}
}
在我看来,对你来说最好的事情是使用Properties类。使用属性类,您可以设置一些属性,然后再次阅读它们!这是我发现最好解释的链接!
https://www.mkyong.com/java/java-properties-file-examples/
如果您不想这样,您可以简单地将该行拆分为3个字符串,每个字符串包含一个数据。
String data[] = line.split(" ");
现在找到开括号和近括号,得到它们之间的内容。我添加了+ 1和-1,因此它也不会读取大括号,只读取之间的数字。
int begin = string.indexOf('(');
int end = string.indexOf(')');
string.substring(begin + 1, end - 1);
现在只需将您的字符串解析为整数。
int number = Integer.parseInt(thedatathatyouhave);
一个快速的谷歌搜索引导我到这个tutorial on basic java I/O,特别是,有一个section on Scanning演示如何方便地打破(也称为“拆分”或“标记化”)你的输入成小块,并可能以更有条理的方式解析它们。
documentation for the Scanner class有更详细的例子。我想你需要这样的东西:
String input = "p1(x1) p1(x2) imp(p1)";
Scanner s = new Scanner(input);
s.findInLine("(\\w+)\\((\\w+)\\) (\\w+)\\((\\w+)\\) imp\\((\\w+)\\)");
我还没有测试过,但基本上“\\(”用于解析“(”和“\\ w +”用于解析一个或多个字符的任何序列。如果要解析一系列数字,则使用“\\ d +”而不是。
希望这有帮助,祝你好运!