【问题标题】:Expanding abbreviations from .csv file从 .csv 文件扩展缩写
【发布时间】:2021-11-16 10:39:33
【问题描述】:

所以我有这个任务要做,我需要将消息中的文本缩写扩展为 .csv 文件中的完整形式,我将该文件加载到 HashMap 中,键作为缩写,值作为完整形式。有一个循环遍历键和 if 语句,如果找到的话,它会将缩写替换为完整形式。我有点想通了,它可以正常工作,但我想将这个更改的字符串(扩展缩写)发送到 if 语句之外的其他地方,以将完整消息保存到文件中。我知道这个字符串只存在于这个 if 语句中,但也许还有另一种方法呢?或者也许我做错了什么?我对 Java 有点生疏了,所以也许有一个我不知道的简单解释。这是我的代码:

public class AbbreviationExpander {

    static void AbrExpander(String messageBody) {

        //read the .csv file

        String csvFile = "textwords.csv";
        String line = "";
        String cvsSplitBy = ",";
        String bodyOut = messageBody;

        HashMap<String, String> list = new HashMap<>();


        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {


                String[] abbreviatonFile = line.split(cvsSplitBy);

                //load the read data into the hashmap

                list.put(abbreviatonFile[0], abbreviatonFile[1]);


            }
            for (String key : list.keySet()) {


                //if any abbreviations found then replace them with expanded version

                if (messageBody.contains(key)) {

                    bodyOut = bodyOut.replace(key, key + "<" + list.get(key).toLowerCase() + ">");


                    try {
                        File file = new File("SMS message" + System.currentTimeMillis() + ".txt");
                        FileWriter myWriter = new FileWriter(file);
                        myWriter.write(bodyOut);
                        myWriter.close();
                    } catch (IOException e) {
                        System.out.println("An error occurred.");
                        e.printStackTrace();
                    }


                }


            }


        } catch (IOException f) {
            f.printStackTrace();
        }


    }


}

【问题讨论】:

    标签: java loops csv if-statement abbreviation


    【解决方案1】:

    不确定我是否理解您的问题。但我认为您应该将代码中的不同步骤分开。

    我的意思是你的try-catch 块写入你的输出应该在for-loop 之外和阅读try-catch 之外。你的for-loop 应该在你的阅读范围之外try-catch

    public class AbbreviationExpander {
    
        static void AbrExpander(String messageBody) {
    
            String csvFile = "textwords.csv";
            String line = "";
            String cvsSplitBy = ",";
            String bodyOut = messageBody;
            HashMap<String, String> list = new HashMap<>();
    
            //read the .csv file
            try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
                while ((line = br.readLine()) != null) {
                    String[] abbreviatonFile = line.split(cvsSplitBy);
    
                    //load the read data into the hashmap
                    list.put(abbreviatonFile[0], abbreviatonFile[1]);
                }
            } catch (IOException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
            }
    
            //if any abbreviations found then replace them with expanded version
            for (String key : list.keySet()) {
                if (messageBody.contains(key)) {
                     bodyOut = bodyOut.replace(key, key + "<" + list.get(key).toLowerCase() + ">");
                }
            }
    
            //output the result in your file
            try {
                File file = new File("SMS message" + System.currentTimeMillis() + ".txt");
                FileWriter myWriter = new FileWriter(file);
                myWriter.write(bodyOut);
                myWriter.close();
            } catch (IOException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 好的,我检查了它,它正在工作。我认为我在这个问题上花费了太多时间并且过于复杂了。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2011-02-16
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多