首页 > 解决方案 > 用arraylist中的单词替换txt文件中的单词?

问题描述

我有一个程序询问用户问题并将它们存储到数组列表中。我想获取我的文本文件并将以 ] 结尾的单词替换为我之前的 arraylist 中的单词。这只是我的一段代码,我在其中扫描文本文件并找到以 ] 结尾的单词并打印它们。(对不起,如果我的代码有点混乱,我对 java 还是有点陌生​​。)

//Scanning the chosen story file for story and custom words
    Scanner sf = new Scanner(new File("vacation.txt"));

    while (sf.hasNextLine()) {
    String current = sf.nextLine();

    String all_words[]; 
    all_words = current.split(" ");

    List<String> mywordList = new ArrayList<String>(all_words.length);
    for (String s:all_words) {
        mywordList.add( s );
    }
    for (String s : mywordList)
    {
        if (s.endsWith("]"))
        System.out.print( s + " " );

标签: java

解决方案


推荐阅读