2014-10-20 154 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)); 
} 
相关问题