如何完成我的文件处理程序?

时间:2015-11-08 04:41:51

标签: java

您好我对Java很陌生,我不知道如何完成我的代码。我正在尝试编写一个读取.txt文件的程序并修复文本中的任何间距问题。我编写了一个代码,在每个单词之间放置一个空格,但我不知道如何在。,!或?之后放置两个空格?

这是我到目前为止所做的:

$scope.selectServerToOpen = function (serverType) {
    var newScope = $scope.$new(true);
    newScope.newServer = $scope.newServer;
    $scope.closeThisDialog('cancel');//coment this line, the data can be passed correctly, but i need to close the parent dialog when i open the child dialog
    modalInstance = ngDialog.open
                template: 'servers/templates/servers-new.tpl.html',
                scope: newScope,
                controller: 'newServerCtrl',
                resolve: {
                    serverData: function () {
                        return null;
                    },
                    delegate: callbackWrapper
                }
            });

我尝试添加IF语句,但这不起作用。就像我说的那样,我是Java和编程的新手,所以对此问题的任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

最好使用StringTokenizer

import java.io.*;
import java.util.*;

public class Word_Processor {

    public static void main(String[] args)
            throws FileNotFoundException {
        Scanner input = new Scanner(new File("Programming3_text.txt"));
        PrintStream output
                = new PrintStream(new File("Programming3_text_fixed.txt"));
        while (input.hasNextLine()) {
            String text = input.nextLine();
            echoFixed(text, output);
            echoFixed(text, System.out);
        }
    }

    public static void echoFixed(String text, PrintStream output) {
        Scanner data = new Scanner(text);
        StringTokenizer tokenizer = new StringTokenizer(text, " .,!?");//put the replaceable characters here DON'T FORGET ' '(space) THERE IS ONE JUST AT THE START OF " .,!?" BEFORE '.'
        String outputLine="";
        while (tokenizer.hasMoreTokens()) {
            outputLine = outputLine + tokenizer.nextToken() + " ";
        }
        output.println(outputLine.trim());
        /* Previous Code
         if (data.hasNext()) {
         output.print(data.next());
         while (data.hasNext()) {
         output.print(" " + data.next());
         }
         }
         output.println();*/
    }
}

如果有效,请将其标记为解决方案。