0

これが私の出力です:「4つのスコアと7年前....」

ただし、期待される入力は次のとおりです。

先頭の空白を保持するにはどうすればよいですか? これまでの私のコードは次のとおりです。

public void checkDocument(Reader in, InputStream input, Writer out) throws IOException {
Scanner sc = new Scanner(input);
TokenScanner tok=new TokenScanner(in);
Token too;
Set<String> fileCor=new HashSet<String>();
while (tok.hasNext()){
    too=tok.next();
    System.out.println(too);
    if (too.isWord()){
        if (!(dict.isWord(too.toString()))){
            int n=1;
            fileCor=corr.getCorrections(too.toString());
            List<String> sortedOptions=new LinkedList<String>(fileCor);
            Collections.sort(sortedOptions);
            System.out.println("The word: "+too.toString()+" is not in the dictionary. Please enter the number corresponding with the appropriate action:");
            System.out.println("0: Ignore and continue");
            System.out.println("1: Replace with another word");
            for (int i=0;i<sortedOptions.size();i++){
                n=n+i+1;
                System.out.println((i+2)+": Replace with \""+sortedOptions.get(i)+"\"");
                }
            int choice=getNextInt(0, n, sc);
            if (choice==0){

            }
            else if (choice==1){
                out.write(getNextString(sc).trim());
            }
            else out.write(sortedOptions.get(choice-2).trim());

        }
        else out.write(too.toString().trim());

    }
    else {
        out.write(too.toString());
    }

}
System.out.println("Document completed");

 }
}
4

1 に答える 1

0

Trim()先頭と末尾の空白を省略して、文字列のコピーを返します。

したがって、末尾のスペースのみを削除する必要がある場合は、以下の正規表現を使用してください。

String newstr = str.replaceAll("\\s*$","");
于 2012-04-12T02:53:50.730 に答える