here is code:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class mat1 {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("rfg.txt"));
List
list.add(new double[] { Double.parseDouble(line[0]),Double.parseDouble(line[1]),Double.parseDouble(line[2]),Double.parseDouble(line[3]) }); } int numberOfRows = list.size(); int numberOfColumns =4; double[][] floatValues = new double[numberOfRows][numberOfColumns]; for (int i = 0; i < numberOfRows; i++) { for(int j=0;j<numberOfColumns;j++){ floatValues[i] = list.get(i); System.out.print(floatValues[i][j] + " "/* + floatValues[i][1] + " " + floatValues[i][2]*/); } System.out.println(); }
}}
here is .txt file:
1.0 3.1 2.1 3.4 2.4 2.0 4.6 5.1 0.2 3.3 4.7 9.1 4.0 5.4 5.1 3.2 6.1 2.1 6.4 2.6
here in program i have to specify no. of columns every time. it is not the case. but i want to print the file of any dimensions in the form of 2d n*m matrix. pls help me
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mat1 { public static void main(String[] args) throws IOException { Scanner s = new Scanner(new File("rfg.txt")); List<double[]> list = new ArrayList<double[]>(); while (s.hasNextLine()) { String[] line = s.nextLine().split(" "); list.add(new double[] { Double.parseDouble(line[0]), Double.parseDouble(line[1]), Double.parseDouble(line[2]), Double.parseDouble(line[3]) }); } int numberOfRows = list.size(); int numberOfColumns = 4; double[][] doubleValues = new double[numberOfRows][numberOfColumns]; for (int i=0 ; i < doubleValues.length ; i++) { doubleValues[i] = list.get(i); System.out.println(); for (int j=0 ; j < doubleValues[i].length ; j++){ System.out.print(doubleValues[i][j]+" "); } } } }
Ads