Hello! Help me to understand. It is necessary to read a text document with a matrix of characters and a number at the end. Here is an example:

o..o......ooooo..oo.. ....................o .o.....oooo..oo..o... 54 

The matrix of symbols, respectively, must be written into a two-dimensional matrix or list, and the symbols, for example, will be true (o) -false (.), Or byte 0 (.) - 1 (o), or enum, not the essence. The number at the end must be written to the variable.

I was able to calculate only the matrix size, width and height:

 private int width = 0; private int height = 0; public void read(){ try(BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\life\\test.txt"))) { String value; while ((value = bufferedReader.readLine()) != null) { if (width == 0) { width = value.length(); } height++; } System.out.println("Ширина:" + width + " Высота:" + (height-1)); } catch (IOException e) { e.printStackTrace(); } } 

I tried the read () method, but it goes through all the characters at once until the end of the file.
Advise how to do?

  • In C #: File.ReadLines (filename) .Select (s => s.Select (c => c == '.')) --- Ah, yes, there’s one more line. Then this: var lines = File.ReadAllLines (filename); var matrix = lines. Take (lines. Count - 1) Select (s => s. Select (c => c == '.')); var num = int.Parse (lines.Skip (lines.Count - 1) .Single ()); - VladD
  • Thank you all for the answers, looking at them, I did the following: String currentLine; int row = 0; while (bufferedReader.ready ()) {currentLine = bufferedReader.readLine (); if (row <(height-1)) {for (int i = 0; i <width; i ++) {cells [row] [i] = currentLine.charAt (i); }} else {generation = Integer.parseInt (currentLine); } row ++; } - Ruslan Lozitsky

2 answers 2

something like this:

 public class ReadMatrix { private int number; private boolean [][] matrix; public ReadMatrix ( int number, boolean [][] matrix ) { setNumber ( number ); setMatrix ( matrix ); } public boolean [][] getMatrix () { return matrix ; } public void setMatrix ( boolean [][] matrix ) { this.matrix = matrix ; } public int getNumber () { return number ; } public void setNumber ( int number ) { this.number = number ; } @Override public String toString () { return toString(matrix) + "\n" + number; } private String toString ( boolean [][] matrix ) { // or new StringBuilder (matrix.length * matrix [0].length) StringBuilder result = new StringBuilder () ; String [] lines = new String [matrix.length] ; for ( int i = 0 ; i < lines.length ; i++ ) { if (i != 0) { result.append ( "\n" ); } result.append ( toString(matrix[i]) ); } return result.toString () ; } private String toString ( boolean [] bs ) { StringBuilder result = new StringBuilder (bs.length) ; for ( int i = 0 ; i < bs.length ; i++ ) { result.append ( bs [i] ? 'o' : '.' ) ; } return result.toString (); } public static void main ( String [] args ) throws FileNotFoundException { // ReadMatrix matrix = getInstance ( System.in) ; ReadMatrix matrix = getInstance ( new FileInputStream ( "C:/test.txt" ) ) ; System.out.println (matrix) ; } public static ReadMatrix getInstance(InputStream inputStream) { // read file String[] lines = readFile(inputStream); // parse input int number = Integer.parseInt ( lines [lines.length - 1] ) ; boolean [][] matrix = null; for ( int i = 0 ; i < lines.length - 1 ; i++ ) { boolean [] tmp = parseLine(lines[i]); if (i == 0) { matrix = new boolean [lines.length][tmp.length]; } matrix[i] = tmp; } return new ReadMatrix (number, matrix); } private static String [] readFile ( InputStream inputStream ) { List<String> result = new LinkedList < String > (); Scanner scanner = new Scanner ( inputStream ); while (scanner.hasNextLine ()) { result.add ( scanner.nextLine ().trim () ); } return result.toArray ( new String [result.size ()] ) ; } private static boolean [] parseLine ( String line ) { char [] chars = line.toCharArray () ; boolean [] result = new boolean [chars.length]; for ( int i = 0 ; i < result.length ; i++ ) { result[i] = (chars[i] == 'o'); } return result ; } } 

    Look what happened with me. The data is taken from stdin.

    Someone else would say how to describe the type of an array of arrays boolean in java, it would be easier to write. This is beyond my understanding of this miracle of the end of the last century. Language, of course, not for the faint of heart. Scary.

    Yes, ArrayList <boolean []> objections should not have been converted to an array - are not accepted. I just wanted to see what happens.

     import java.util.ArrayList; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; class RPC { public static void main (String args []) { int width=0, height=0; ArrayList<boolean []> matrix = new ArrayList<boolean []> (); int number = 0; boolean finish = false; try { BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(System.in)); String value; while ((value = bufferedReader.readLine()) != null) { if(finish) { System.out.println("Непонятный хвост " + value); return; } value = value.trim(); if(value.charAt(0) == 'o' || value.charAt(0) == '.') { if (width == 0) { width = value.length(); } boolean [] tmp = new boolean [width]; for(int i = 0; i < value.length(); i++) tmp[i] = value.charAt(i) == 'o'; matrix.add((boolean [])tmp.clone()); height++; } else { finish = true; number = Integer.parseInt(value); } } Object [] matrix_ = matrix.toArray(); System.out.println("Ширина:" + width + " Высота:" + height); for(int h=0; h < height; h++) { boolean [] matrixLine = (boolean []) matrix_ [h]; for(int w=0; w < width; w++) System.out.print((matrixLine[w] ? 't' : 'f') + " "); System.out.println(); } System.out.println("Число =" + number); } catch (IOException e) { e.printStackTrace(); } } }