2016-10-13 3 views
0

私は段落をきれいに印刷するためにJavaプログラムを作成しています。段落を別のファイルにきれいに印刷

private static void printSolution(String[] words, int[] print, int n,int width) { 
    if (n >= 0) { 
     printSolution(words, print, print[n] - 1, width); 
     int index = print[n]; 
     System.out.printf("%s", words[index]); 
     width -= words[index].length(); 
     ++index; 
     for (; index <= n; index++) { 
      System.out.printf(" %s", words[index]); 
      width -= words[index].length() + 1; 
     } 

     while (width > 0) { 
      System.out.print(" "); 
      width--; 
     } 

     System.out.println("|"); 
    } 
} 

は今、私はそれを少し変更して、別のファイル(output.txtと)への出力を印刷したいのですが、私は:それは今あるようにようにしかし、プログラムはコンソールに出力しますそのおなじみのJavaではないので、私はこれに助けが必要です。だから、私はどのように行うか分からないのは、結果をStringに格納し、printfと同じようにフォーマットすることです。

答えて

1

あなたは、例えばので、この行を

をフォーマット文字列を行うにはString.Formatの()を使用することができます。

System.out.printf("%s", words[index]); 

は、次のように行うことができます。

String newStr = String.format("%s", words[index]); 

そして、あなたのことができ残りの行をnewStrに追加します。 newStringをファイルに書き込む場合は、BufferedWriterを使用します。

また、あなたのコードの呼び出しはここ

printSolution(words, print, print[n] - 1, width); 
+0

StackOverflowの例外がスローされますので、ありがとうございました!!非常に明確な説明! – useruser1412

関連する問題