2012-04-12 78 views
0

這裏的前導空格是我的輸出:「四個分值87年前......」我的令牌似乎跳過一個文本文件

但是,預期的輸入是:「四個分值七年前......「(帶引號和‘四’之間的空間」。

我如何保持領先的空白?這裏是我到目前爲止的代碼。

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"); 

} 
} 
+1

[Trim()](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#trim())返回字符串的副本,省略了前導和尾隨的空白。可能是這個問題。 – RanRag 2012-04-12 02:47:52

回答

0

Trim()返回副本的字符串,省略了前導空白和尾隨空白。

因此,如果您只需要刪除尾部空格,請使用下面的正則表達式。

String newstr = str.replaceAll("\\s*$","");