fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone {
  9.  
  10. public static void main (String[] args) throws java.lang.Exception {
  11.  
  12. int width;
  13. int height;
  14. List<char[]> map;
  15.  
  16. // pretend this is opening the real file, i just had to embed the data
  17. // as a string because ideone.
  18. BufferedReader br = new BufferedReader(new StringReader(mapFileData));
  19.  
  20. // initialize
  21. width = 0;
  22. map = new ArrayList<char[]>();
  23.  
  24. String line;
  25. while ((line = br.readLine()) != null) {
  26. // optional: skip blank lines:
  27. if (line.length() == 0)
  28. continue;
  29. // optional: verify that all lines are same length:
  30. if (width == 0)
  31. width = line.length();
  32. else if (line.length() != width)
  33. throw new Exception("lines are not all the same length.");
  34. // this is the only thing you really *need* to do:
  35. map.add(line.toCharArray());
  36. }
  37.  
  38. height = map.size(); // height is number of lines
  39.  
  40. System.out.printf("size: %d x %d\n", width, height);
  41. for (int y = 0; y < height; ++ y) {
  42. System.out.printf("%3d: ", y);
  43. for (int x = 0; x < width; ++ x)
  44. System.out.print(map.get(y)[x]);
  45. System.out.println();
  46. }
  47.  
  48. }
  49.  
  50. static String mapFileData =
  51. "#########################\n" +
  52. "#.......................#\n" +
  53. "#......#................#\n" +
  54. "########................#\n" +
  55. "#......#................#\n" +
  56. "#......#................#\n" +
  57. "#......#................#\n" +
  58. "#.######................#\n" +
  59. "#.......................#\n" +
  60. "#########################\n";
  61.  
  62. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
size: 25 x 10
  0: #########################
  1: #.......................#
  2: #......#................#
  3: ########................#
  4: #......#................#
  5: #......#................#
  6: #......#................#
  7: #.######................#
  8: #.......................#
  9: #########################