2014-10-20 153 views
1

我有三個運行參數,即最小寬度,最大寬度和文本文件名。文本文件用一長串隨機字符填充。我想把每個角色放入一個網格點。但是我所得到的只是文件中的字符串本身。我如何製作網格?從文本文件中的字符串創建網格

class GridCipher{ 

    static int minGridWidth; 
    static int maxGridWidth; 

    static File inputFile; 

    public static void main(String[] args) throws FileNotFoundException { 
     if (handleArguments(args)) 
     processInput(); 

    } 

    static final String usage = "Usage: GridWriter min_width max_width input_file_name"; 

    static boolean handleArguments(String[] args) { 
     // Check for correct number of arguments 
     if (args.length != 3) { 
     System.out.println("Wrong number of command line arguments."); 
     System.out.println(usage); 
     return false; 
     } 

    try { 
     minGridWidth = Integer.parseInt(args[0]); 
     maxGridWidth = Integer.parseInt(args[1]); 
     } catch (NumberFormatException ex) { 
     System.out.println("min_width and max_width must be integers."); 
     System.out.println(usage); 
     return false; 
     } 

     inputFile = new File(args[2]); 
     if (!inputFile.canRead()) { 
     System.out.println("The file " + args[2] + " cannot be opened for input."); 
     return false; 
     } 

     return true; 
    } 

    static void processInput() throws FileNotFoundException { 
     Scanner input = new Scanner(inputFile); 
     String line = input.nextLine(); 
     int length = line.length(); // number of characters. 

    // Try each width in the appropriate range 
     for (int width = minGridWidth; width <= maxGridWidth; width++) { 
     // Determine heigth of grid 
     int height = line.length()/width; 

     // Add one to height if there's a partial last row 
     if (line.length() % width != 0) 
      height += 1; 

     loadUnloadGrid(line, width, height); 
     } 
    } 

    static void loadUnloadGrid(String line, int width, int height) { 
     char grid[][] = new char[height][width]; 

     // Determine number long columns 
     int longColumn = line.length() % width; 
     if (longColumn == 0) 
     longColumn = width; 

     //Load the input data into the grid by column 
     int charCount = 0; 
     for (int c = 0; c < width; c++) { 
     for (int r = 0; r < height; r++) { 
      if (r < height - 1 || c < longColumn) { 
       grid[r][c] = line.charAt(charCount); 
       charCount += 1; 
      } 
     } 
     } 

     // Output data from the grid by rows 
     for (int r = 0; r < height - 1; r++) { 
     for (int c = 0; c < width; c++) { 
      System.out.print(grid[r][c]); 
     } 
     } 
     // Special handling for last row 
     for (int c = 0; c < longColumn; c++) { 
     System.out.print(grid[height - 1][c]); 
     } 
     System.out.println("\""); 
    } 
} 

如果文本文件有ABCDE,我就回到ABCDE。我希望網格中的字符由我的最小和最大寬度決定。

+0

你能對我這樣的人你怎麼在我的最小和最大寬度確定的網格意味着字符解釋一下嗎? – 2014-10-20 05:56:09

+0

您是否嘗試過調試代碼? – 2014-10-20 05:57:19

+0

如果我選擇寬度爲2,那麼網格將是AB,CD,E留下一個額外的空間。 – Jordan 2014-10-20 05:59:45

回答

0

如果我正確理解你想要的ABCDEFG到

A B C 
D E F 
G 

但片段在屏幕上書寫它,你會錯過新行字符

// Output data from the grid by rows 
     for (int r = 0; r < height - 1; r++) { 
     for (int c = 0; c < width; c++) { 
      System.out.print(grid[r][c]); 
     } 
     } 

應該

// Output data from the grid by rows 
     for (int r = 0; r < height - 1; r++) { 
     for (int c = 0; c < width; c++) { 
      System.out.print(grid[r][c]); 
     } 
     System.out.println(); 
     } 
0

印刷網格在您的程序中不正確。

更改下面

// Output data from the grid by rows 
for (int r = 0; r < height - 1; r++) { 
    for (int c = 0; c < width; c++) { 
     System.out.print(grid[r][c]); 
    } 
} 

這個代碼

for(char[] arr : grid) { 
    System.out.println(Arrays.toString(arr)); 
} 
相關問題