2012-09-19 72 views
0

我有這樣的代碼,打印空格的值

else if (e.getSource() == saveButton) { 
      Scanner scan; 
      String es = " "; 
      try { 
       int status = chooser.showSaveDialog(null); 
       if (status == JFileChooser.APPROVE_OPTION) { 
        File newImageName = chooser.getSelectedFile(); 
        PrintWriter outFile = new PrintWriter(new FileWriter(newImageName)); 
        outFile.print(drawing.typefile); 
        outFile.print(drawing.iname); 
        outFile.print(drawing.width); 
        outFile.print(es); 
        outFile.print(drawing.height); 
        outFile.print(drawing.maxshade); 
        for(int r = 0; r < drawing.array.length; r++) 
         for(int c = 0; c < drawing.array[r].length; c++) 
          outFile.print(drawing.array[r][c]); 


      //  outFile.print(drawing.paintComponent(newImageName)); 
        outFile.flush(); 
        outFile.close(); 
       } 
      else if (status == JFileChooser.CANCEL_OPTION) { 
       } 
      } catch (Exception ex) { 
        JOptionPane.showMessageDialog(null, "File could not be written, try again."); 
        //ex.printStackTrace(); 
       } 

     } 

和我如何得到它打印空間中的值之間? 它打印

P2image1.pgm320 275255132132130125125128 

,我需要它來打印

P2image1.pgm320 275255132 132 130 125 125 128 

因此有是在每個數組值之間的空間,我已經嘗試了很多東西,但我徹底糊塗了。

謝謝。

+0

更簡單的辦法就是把和outFile.print(」「);每個打印語句之間。 – rlinden

+1

'for(int c = 0; c

回答

1

只需在每個數字後面明確地打印空格。

你有沒有嘗試過這樣的:

for(int r = 0; r < drawing.array.length; r++) { 
    for(int c = 0; c < drawing.array[r].length; c++) { 
     outFile.print(drawing.array[r][c]); 
     outFile.print(" "); 
    } 
} 
+0

括號內的+1,我懷疑這是OP「嘗試了很多東西」時丟失的重要東西。 –

+0

非常感謝,這幫了我很大的忙 – Ignacious

0
outFile.print(drawing.array[r][c]); 
outFile.print(" "); 
+0

隨着代碼的編寫方式,這將導致在EOF只有一個空間。 –