【问题标题】:Java: Replacing String with IntegerJava:用整数替换字符串
【发布时间】:2017-04-25 07:13:30
【问题描述】:

我是新来的,所以我希望我能正确描述这个问题,并在我更喜欢 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,直到找到正确的键。所以我删除了第一个while-loop中的i++,所以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

【问题讨论】:

  • 你有if (line.startsWith("%")) {//every graph starts with % and a number so i know which graph is currently in progress,但你的输入中没有%
  • 问题是这样的图有2000个。图 3 f.e.得到3_i_,图44_i_等等。数字也需要按照正确的顺序从 1 到有多少。我尝试使用编辑器,但需要的时间太长。
  • 您能否提供一个更完整的示例,因为目前看起来中间列是一个计数器?我怀疑不需要多次读取文件。
  • 在我的文件中,每个图表都以%x 之类的开头。不同之处在于 x 代表从 1 到 2000 的数字。忘记插入了,对不起。 ._.
  • @PeterLawrey 我编辑了我的帖子,所以最后你可以看到前 3 个完整的图表。这行得通吗?

标签: java string replace hashmap integer


【解决方案1】:

这可以作为一个起点。拆分字符串似乎是一种更好的方法。

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
}

输出

【讨论】:

    【解决方案2】:

    我将此部分添加到您的代码中

    ArrayList <String> strings = new ArrayList<>();
    int counter=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++;
                counter++;
                strings.add(line.split(" ")[0]+" "+counter+" "+line.split(" ")[2]);
                replacements.put(line.split(" ")[1], counter);
            }
        }
    fr.close();
    strings.forEach(System.out::println);
    

    如您所见,我添加了一个 counter 和一个名为 stirngs 的数组列表,就在您的替换哈希映射之前,我确实收集了字符串的第一部分和最后一部分,而不是像您那样取中间部分并打印在 @ 987654324@

    【讨论】:

      【解决方案3】:

      您的全部替换逻辑不正确。

      3_i_1 映射到 3

      它也匹配 3_i_15 的 replaceAll 大小写,这就是为什么您看到它被替换为 34、35、36

      我的 file.txt 中有两个图表,第二个图表显示了问题。一种解决方法是在全部替换子句中附加一个 " " 空格,以区分 3_i_1 和 3_i_15

      由于您正在迭代哈希图的条目,因此看起来像 2_i_15,8 出现在 2_i_1,3 之前,而 3_i_1,3 出现在 3_i_15,8 之前。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-27
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多