java.util.scanner - Reading Text File in java while preserving white spaces using scanner class -


I want to read this text file and store the data in a 2D array. Each white spot should be replaced with 0 , which will result in the final matrix of 0 and 1. The problem that is happening is that the scanner ignores all the white spaces and only put 1s in the matrix.

This is the text / input file. <11 p> 111 1 11 11111 111 111 1111 111 1111 111 1111 11111 111111 111 11 11 11111 111 1111 11 11111 1111 11111 1111 11 11 1111 111 111 111 111 11 1111 111111 1111 111 1111 1111 111 1111 1111 11 1111 1111 1111 1111 11 1111 1111 1111 111111 1111111 11111 1111 1111

Scanner file scanner = new scanner (new file ("D: \ Assignment .txt")); Int [] [] Input matrix = new int [200] [200]; Int i = 0, j = 0; While (fileScanner.hasNextLine ()) {string line = file scanner.nxtain (); Scanner line scanner = new scanner (line); While (lineScanner.hasNext ()) {int token = integer. Parasont (lineScanner.next ()); InputMatrix [i] [j] token =; // token should be done with J ++; } LineScanner.close (); I ++; j = 0; // You are here at the end of the line what you have to do. } FileScanner.close ();

Read the rows and convert them into 2D array:

  List & lt; String & gt; Line = new archilist & lt; & Gt; (); Scanner sc = new scanner (new file ("array.txt")); While (sc.hasNextLine ()) {lines.add (sc.nextLine ()); } // array int lines = lines.size (); // Number of rows int cols = 0; // Number of columns (string line: line) {cols = Math.max (cols, line.length ()); } Int [] [] array = new int [rows] [cols]; // is equal to ii = 0; (String line: lines) {// for each line, 1s four [] additions [] chars = line.toCharArray (); For (int j = 0; j and lt; chars; long; ++ j) {if (character [j] == '1') {array [i] [j] = 1; }} I ++; }    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -