gpt4 book ai didi

Java:用整数替换字符串

转载 作者:行者123 更新时间:2023-11-30 08:30:39 26 4
gpt4 key购买 nike

我是新来的,所以我希望我能正确描述这个问题,并在我更喜欢 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 , 所以 i0每时每刻。得到了这样的东西:

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

enter image description here

关于Java:用整数替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com