我是新来的,所以我希望我能正确描述这个问题,并在我更了解 Java 之类的东西时帮助其他人。
但这就是我来这里的原因...我需要转换给定的文本文件,这样我就可以在名为“SUBDUE”的程序中使用它。
文件中给定图表的格式如下所示:
v 2_i_6 startevent
v 2_i_7 endevent
v 2_i_1 task
v 2_i_2 task
v 2_i_3 task
v 2_i_4 task
v 2_i_5 task
v 2_i_15 and
v 2_i_17 and
v 2_i_14 xor
v 2_i_16 xor
v 2_i_18 xor
v 2_i_19 xor
但是对于程序来说,该文件中的图表需要如下所示:
v 1 startevent
v 2 endevent
v 3 task
v 4 task
v 5 task
v 6 task
v 7 task
v 8 and
v 9 and
v 10 xor
v 11 xor
v 12 xor
v 13 xor
起初我以为我会手工完成。但我意识到有2000个。所以我尝试用Java编写一个程序。
这就是我得到的:
public class Input {
HashMap<String, Integer> replacements = new HashMap<String, Integer>();
public void readFile() throws IOException {
FileReader fr = new FileReader(
"file.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
StringBuilder sb = new StringBuilder();
int i = 0;
while ((line = br.readLine()) != null) {
if (line.startsWith("%")) {//every graph starts with % and a number so i know which graph is currently in progress
i = 0;
}
if (line.startsWith("v")) {
i++;
replacements.put(line.split(" ")[1], i);
}
}
fr.close();
fr = new FileReader("file.txt");
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
for (Entry<String, Integer> e : replacements.entrySet()) {
line = line.replaceAll(e.getKey().toString(), e.getValue().toString());
line.toString();
}
sb.append(line);
System.out.println(line);
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
fr.close();
FileWriter fw = new FileWriter(
new File("file.txt"));
fw.write(sb.toString());
fw.close();
}
当我启动程序时,它大部分转换正确,但有时会发生这种情况:
v 1 startevent
v 2 endevent
v 3 task
v 4 task
v 5 task
v 6 task
v 7 task
v 35 and
v 37 and
v 34 xor
v 36 xor
v 38 xor
v 39 xor
我开始调试,但放弃了,因为我需要遍历整个 map ,直到找到正确的键..所以我删除了 i++
在第一while-loop
,所以i
是0
每时每刻。并得到了这样的东西:
v 0 startevent
v 0 endevent
v 0 task
v 0 task
v 0 task
v 0 task
v 0 task
v 05 and
v 07 and
v 04 xor
v 06 xor
v 08 xor
v 09 xor
但我不知道为什么它只替换了两位数的第一个数字。
我希望有人给我一个提示,或者更好的选择来按照我想要的方式替换这些字符串。
感谢您的回答。
因为这里要求的是我文件的前三张图表。很抱歉给您带来不便,
%1./konvertiermich/andere/Paper-Leben-Modeliranje_delovnih_procesov/obdelava_narocil.epml.pl
v 1_i_2 startevent
v 1_i_17 endevent
v 1_i_23 endevent
v 1_i_26 endevent
v 1_i_4 task
v 1_i_5 task
v 1_i_9 task
v 1_i_11 task
v 1_i_20 task
v 1_i_21 task
v 1_i_25 task
v 1_i_6 xor
v 1_i_10 xor
v 1_i_13 xor
v 1_i_14 xor
v 1_i_15 xor
v 1_i_19 xor
v 1_i_22 xor
d 1_i_2 1_i_4 arc
d 1_i_5 1_i_6 arc
d 1_i_9 1_i_10 arc
d 1_i_10 1_i_14 arc
d 1_i_10 1_i_15 arc
d 1_i_11 1_i_13 arc
d 1_i_13 1_i_14 arc
d 1_i_13 1_i_15 arc
d 1_i_20 1_i_17 arc
d 1_i_19 1_i_21 arc
d 1_i_21 1_i_22 arc
d 1_i_22 1_i_23 arc
d 1_i_25 1_i_26 arc
d 1_i_4 1_i_5 arc
d 1_i_6 1_i_9 arc
d 1_i_6 1_i_19 arc
d 1_i_10 1_i_11 arc
d 1_i_15 1_i_20 arc
d 1_i_14 1_i_19 arc
d 1_i_22 1_i_25 arc
%2./konvertiermich/andere/Web-Wikipedia.cz/wikipedia.cz.epml.pl
v 2_i_6 startevent
v 2_i_7 endevent
v 2_i_1 task
v 2_i_2 task
v 2_i_3 task
v 2_i_4 task
v 2_i_5 task
v 2_i_15 and
v 2_i_17 and
v 2_i_14 xor
v 2_i_16 xor
v 2_i_18 xor
v 2_i_19 xor
d 2_i_1 2_i_14 arc
d 2_i_3 2_i_18 arc
d 2_i_6 2_i_3 arc
d 2_i_14 2_i_7 arc
d 2_i_15 2_i_5 arc
d 2_i_16 2_i_1 arc
d 2_i_17 2_i_4 arc
d 2_i_17 2_i_2 arc
d 2_i_19 2_i_17 arc
d 2_i_2 2_i_15 arc
d 2_i_4 2_i_15 arc
d 2_i_18 2_i_19 arc
d 2_i_5 2_i_16 arc
d 2_i_14 2_i_19 arc
d 2_i_18 2_i_16 arc
%3./konvertiermich/deutsch/BA-Blau-Customer_Relationship_Management_gestützte_Prozesse_am_Beispiel_des_Unternehmens_Alere/39-Angebotsprozess.pl
v 3_i_1 startevent
v 3_i_19 endevent
v 3_i_2 task
v 3_i_6 task
v 3_i_7 task
v 3_i_10 task
v 3_i_13 task
v 3_i_15 task
v 3_i_18 task
v 3_i_3 xor
v 3_i_8 xor
v 3_i_12 xor
v 3_i_17 xor
d 3_i_1 3_i_2 arc
d 3_i_2 3_i_3 arc
d 3_i_6 3_i_8 arc
d 3_i_7 3_i_8 arc
d 3_i_12 3_i_13 arc
d 3_i_17 3_i_18 arc
d 3_i_18 3_i_19 arc
d 3_i_12 3_i_17 arc
d 3_i_3 3_i_6 arc
d 3_i_3 3_i_7 arc
d 3_i_8 3_i_10 arc
d 3_i_10 3_i_12 arc
d 3_i_13 3_i_15 arc
d 3_i_15 3_i_17 arc
最佳答案
这可以作为起点。分割字符串似乎是更好的方法。
public static void main(String[] args) {
// TODO code application logic here
String [] input = {"v 2_i_6 startevent", "", "v 2_i_7 endevent"};
for(int i = 0; i < input.length; i++)
{
System.out.println(myStringFormatter(input[i]));
}
}
static String myStringFormatter(String input)
{
if(input.length() > 0)
{
String[] processing = input.split(" ");//Splits the string into 3 parts v, 2_i_6, and startevent
String[] processing2 = processing[1].split("_");//Splits 2_i_6 into 3 parts 2, i, and 6
return processing[0] + " " + processing2[2] + " " + processing[2]; //Takes the first part of the first split, the third part of the second split, and the third part of the first split and put them back together separated by a space
}
return "";//If it gets an empty string, its going to return an empty string
}
Output
关于Java:用整数替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066667/