java - printing content of array row wise & columnwise -


here code:

import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.list; import java.util.scanner; public class readfilecontents {     public static void main(string[] args) throws ioexception {     scanner s = new scanner(new file("rfg.txt"));     list<float[]> list = new arraylist<float[]>();     while (s.hasnextline()) {         string[] line = s.nextline().split(" ");         list.add(new float[] { float.parsefloat(line[0]),float.parsefloat(line[1]),float.parsefloat(line[2]) });         }           int numberofrows = list.size();         int numberofcolumns = 3;          float[][] floatvalues = new float[numberofrows][numberofcolumns];         (int = 0; < numberofrows; i++) {             floatvalues[i] = list.get(i);             system.out.println(floatvalues[i][0] + " " + floatvalues[i][1] + " " + floatvalues[i][2]);             }         }     } 

here .txt file:

5.1 3.5 1.4 2.0      4.9 3.0 1.4 40.1     4.7 3.2 1.3 1.4      4.6 3.1 1.5 5.1              5.0 3.6 1.4 4.1                  5.4 3.9 1.7 9.4          4.6 3.4 1.4 4.5          5.0 3.4 1.5 3.51         4.4 2.9 1.4 4.0          4.9 3.1 1.5 1.45  

it gives o/p of 3 columns:

but want print file having same dimension given in file(file "n*m" dimension). dimensions can changed per given file.

you reading 3 columns text file. if number of columns dynamic can use loop this:

  while (s.hasnextline()) {     string[] line = s.nextline().split(" ");     float[] f = new float[line.length];     for(int = 0; < line.length; i++){         float[i] = float.parsefloat(line[i]);     }      list.add(f);     numcolumns = line.length;   } 

this way number of columns become number of columns in file.


Comments