有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java用arraylist中的单词替换txt文件中的单词?

我有一个向用户提问并将其存储到arraylist的程序。我想把我的文本文件中以“]结尾的单词替换为之前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 + " " );

共 (0) 个答案