I have convert a txt file into a 2d array, how can I make it look more organize?
My input file look like this:
[Name], Exam1, Exam2, Exam3
John, 99, 88, 89
May, 99, 100, 100
Mary, 100, 100, 100
Peter, 60, 60, 60
Currently I get:
[Name] Exam1 Exam2 Exam3
John 99 88 89
May 99 100 100
Mary 100 100 100
Peter 60 60 60
I want the data looks more like a table which is easier to read, how can I do that?
Code:
public static void main(String[] args) throws Exception{
File file = new File("test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int width = 0, height = 0;
String line = "";
/*Find number of row and column of the file.*/
while ((line = br.readLine()) != null)
{
if (width == 0)
{
/*Find the number of row using split method(",")*/
String[] str = line.split(",");
width = str.length;
}
height++;
}
System.out.println("Row : " + height);
System.out.println("Column : " + width);
/*Adding values to the 2D Array.*/
String[][] data = new String[height][width];
br = new BufferedReader(new FileReader(file));
for (int i = 0; i < height; i++)
{
if ((line = br.readLine()) != null)
{
for (int j = 0; j < width; j++)
{
String[] str = line.split(",");
data[i][j] = str[j];
System.out.print( data[i][j] + " ");
}
}
System.out.println("");
}
}
Thank you very much.
try output using printf
or you can use Formatter
I imagine your not going to want all that System.out stuff in your product but I am going to use it in an example, building on yours to show you the basic steps you will need to do.
Basically you need to make two passes over your data. First pass you should calculate the widest row of the data, so you can craft your ---- line. Then you add the like to whatever output type you are building (here it is System.out) and then walk the data again and add that to the output. You should add something like a newline or other terminator. If you want to line up the columns, you do the same thing but in the first step also record in another multi dimensional array the widest data in each column, and pad data that is shorter that the widest in each column when outputting (this will alter the width of your --- line so you will need to calculate this before building that line of course).
Here is your example modified a little bit with some of these ideas (not padding to line up the columns though, that is for you to do, it is easy trust me)
public static void main(String[] args) throws Exception {
/* Adding values to the 2D Array. */
String[][] data = { { "John", "99", "88", "89" },
{ "May", "99", "100", "100" } };
int wideRowWidth = 0;
int curRowWidth;
// WALK DATA ONCE TO FIND THE WIDEST ROW
for (int i = 0; i < data.length; i++) {
curRowWidth = 0;
for (int j = 0; j < data[i].length; j++) {
curRowWidth += data[i][j].length();
}
if (curRowWidth > wideRowWidth) {
wideRowWidth = curRowWidth;
}
}
// BUILD YOUR DASHED LINE
for (int i = 0; i < wideRowWidth + data[0].length -1; i++) {
System.out.print("-");
}
System.out.print("\n");
// USE YOUR DATA
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.print("\n");
}
}
You can do it in this way, which is as follows :
// To Store and Show Values on the screen.
int columnWidth = 15;
for (int i = 0; i < height; i++)
{
if ((line = br.readLine()) != null)
{
// After reading, we are seperating them.
String[] str = line.split(", ");
for (int j = 0; j < width; j++)
{
data[i][j] = str[j];
// Here we are making the word length of each String equal,
// i.e. equal to column width. So that each String value comes
// below each other. Increase the value of columnWidth variable
// if you want to increase the width of a column or vice versa.
System.out.format("%-" + columnWidth + "s", data[i][j]);
}
}
System.out.print("\n");
}
Hope that might help.
Regards
Related
Im trying to practice some java and I am confused. I am trying to enter in multiple numbers into a 3*3 array, however when I run my program I get a compliation error (Exception in thread "main"java.lang.NumberFormatException)? How can parse multiple ints from a Joptionpane into the arrays?
public static int[][] enterMatrix() {
String NumberstoParse = JOptionPane.showInputDialog("Enter list: ");
int UserInput = Integer.parseInt(NumberstoParse);
int[][] matrix = new int[3][3];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = UserInput;
return matrix;
}
}
I think the main issue is when parsing the String from the JOptionPane. Integer.parseInt() sees the commas and throws NumberFormatException. Might be worthwhile to do some testing of this method, possibly with JShell!
Here, I have taken the input String "1, 2, 3, 4, 5, 6, 7, 8, 9" and used method split from class String to make an array of String that is split by (",\s+"). This means, split around the matching regular expression, which here is "a comma and one or more white space characters". Each individual String from the array is then processed with Integer.parseInt().
public static int[][] enterMatrix() {
String numberstoParse = JOptionPane.showInputDialog("Enter list: ");
String[] splitNumbers = numberstoParse.split(",\\s+");
int[][] matrix = new int[3][3];
int ctr = 0;
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Integer.parseInt(splitNumbers[ctr]);
ctr++;
}
return matrix;
}
Adding to what Alex already added below is the code which will take care of some border line issues there are some test cases include few test cases. The code is documented, I hope this helps...
public class Dummy
{
public static void main(String[] args)
{
String temp = "";
for(int x = 0; x <10; x++){
temp = temp + x+"";
int[][] matrix = enterData(temp);
System.out.println("Given Input:" + temp);
if(matrix != null){
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}
}
System.out.println("-------------");
temp +=",";
}
}
//Once you understand the test cases, you can remove the argument and use the JOptionPane for input
public static int[][] enterData(String input)
{
//TODO: Please user JOPtionPane I have added this just to make the test cases work
//String input = JOptionPane.showInputDialog("Enter list: ");
//This will split the Input on the basis of ","
String[] inputArr = input.split(",");
//Variable has a counter which which will represent the number of inputs received
int inputArrCount = 0;
int[][] matrix = new int[3][3];
//If the size is greater than 9, then as u suggested an error is printed
if(inputArr.length > 9 ){
System.err.println("Number length > 9");
return null;
}
for(int i = 0; i <matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
//If to just track that inputArrCount never goes beyond the inputArr elements
if(inputArrCount < inputArr.length){
int temp = Integer.parseInt(inputArr[inputArrCount++]);
matrix[i][j] = temp;
}
}
}
return matrix;
}
}
The full project is to take data in from a file which is a text file containing a list of all 201 countries and their respective rates of internet use in alphabetical order. Here is an example
Afghanistan 7
Albania 63
Algeria 20
Andorra 97
Angola 23
...
With this we have to Shellsort (specifically) the data numerically. I have successfully done this but I only am outputting a list of the percentages, where as I need the countries listed as well. Here is my code:
import java.io.*;
import java.util.*;
public class InternetUsers {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String populationString = "";
String[] line = new String[201];
int populations[] = new int[201];
Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));
while(fileIN.hasNext()){
for(int i = 0; i < 201; i++){
populationString = fileIN.nextLine().substring(26, 29);
populations[i] = Integer.parseInt(populationString.trim());
}
int j;
for(int gap = populations.length / 2; gap > 0; gap /= 2){
for (int k = 0; k < populations.length; k++){
}
for (int t = gap; t < populations.length; t++){
int tmp = populations[t];
for(j = t; j >= gap && (tmp < populations[j - gap]); j -= gap){
populations[j] = populations[j - gap];
}
populations[j] = tmp;
}
}
System.out.println("\nFinal sorted order: ");
for(int k = 0; k < populations.length; k++){
System.out.print(populations[k]);
System.out.println("");
}
System.out.println();
}
}
}
So my question is how am I to go about outputting the countries as well? do I need to completely redo the way I sorted? Here is my sample output:
Final sorted order:
1
1
2
2
2
2
2
3
....
When you parse the file, you need to store parsed value in a dictionary or some other structure. After you sort, when printing, read the values from dictionary.
I modified you code to store values in a dictionary, and added comments to the lines I added/modified. I did not touch your sorting algo, so you are still sorting on the same array:
public static void main(String[] args) throws IOException {
String populationString = "";
String[] line = new String[201];
int populations[] = new int[201];
// Have a dictionary that can store the values you parse
Map<Integer, String> dictionary = new HashMap<Integer, String>();
Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));
while (fileIN.hasNext()) {
for (int i = 0; i < 201; i++) {
// Parse the whole line, this 29 hard coded seems incorrect
populationString = fileIN.nextLine().substring(0, 29);
// Grab both values
String[] splited = populationString.split("\\s+");
// Country name can have spaces, so take the last elemnt
populations[i] = Integer.parseInt(splited[splited.length - 1]);
// Join back values
String country = populationString.join(" ", splited);
// Cut off the rate number
country = country.substring(0, country.lastIndexOf(" "));
// Store them in your dictionary
if (dictionary.containsKey(populations[i])) {
// If multiple countries have same rate, add them to value, and separate with comma
String value = dictionary.get(populations[i]);
dictionary.put(populations[i], value + "," + country);
} else {
dictionary.put(populations[i], country);
}
}
int j;
for (int gap = populations.length / 2; gap > 0; gap /= 2) {
for (int t = gap; t < populations.length; t++) {
int tmp = populations[t];
for (j = t; j >= gap && (tmp < populations[j - gap]); j -= gap) {
populations[j] = populations[j - gap];
}
populations[j] = tmp;
}
}
System.out.println("Final sorted order: ");
for (int k = 0; k < populations.length; k++) {
// Read the value from dictionary
String value = dictionary.get(populations[k]);
// For duplicates skip, that entry gets deleted after values were printed
if (value == null) {
continue;
}
// If multiple countries had the same rate, they were stored as comma separated value
String[] countries = value.split(",");
for (String country : countries) {
// You can print rate, or country, or both
System.out.println(populations[k] + " " + country);
}
// Remove from dictionary, because we already printed all countries with the same rate
dictionary.remove(populations[k]);
}
System.out.println();
}
// Don't forget to close the file
fileIN.close();
}
Unless prof. said specifically do it with an array of Strings for the countries and an array of ints for the rates, #ScaryWombat's idea of an array of objects each containing a String and an int is the way to go.
That being said, you could still do it with the separate arrays if you must. Just be sure to swap both the line and the population entries when your sort algorithm calls for a swap, not only the population entry.
I have a text file
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
that I want to read into a 2D double array that looks like this.
{{0.4658, 0, 3},
{0.4095, 0, 3},
... (and so on)
{0.4328, 0, 3}}
I have the following code:
public static void main(String[] args) throws Exception{
double[][] ref = null;
ref = matrix("data80.in",10,3);
}
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = null;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
if (matrix == null) {
matrix = new double[size1][size2];
}
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
But it keeps giving me an outOfBounds exception, and I don't know where I am going wrong. Please help. If anyone has more efficient solutions as well to my problem it would be helpful
It's because of the following for loop:
for (int col = 0; col < size1; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
We are using size1 whereas we should be using size2, following would work:
for (int col = 0; col < size2; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
Also, there is no need for null check inside the for loop, you can remove it and initialise the array in the beginning, e.g.:
public static double[][] matrix(String filename, int size1, int size2) throws Exception {
double[][] matrix = new double[size1][size2];;
BufferedReader buffer = new BufferedReader(new FileReader(filename));
String line;
int row = 0;
while ((line = buffer.readLine()) != null) {
String[] vals = line.trim().split("\\s+");
for (int col = 0; col < size2; col++) {
matrix[row][col] = Double.parseDouble(vals[col]);
}
row++;
}
buffer.close();
return matrix;
}
You have defined you 2d matrix as
matrix = new double[size1][size2];
meaning there are size1 rows and size2 columns but in following line:
for (int col = 0; col < size1; col++) {
you have used size1. So correction is:
for (int col = 0; col < size2; col++) {
Here is yet another concept as to how you can place the delimited numerical data contained within a text file of any number of rows and any number of columns into a Double data type Two Dimensional Array. All you need to do is pass the path and file name to the method. You can also optionally supply the delimiter used within the file, the method default is a comma (,) delimiter since it is one of the most commonly used. Here is the method:
public static double[][] matrix(String filename, String... delimiterInFile) {
String delimiter = ","; // Default data delimiter in file.
// See if a optional data delimiter is supplied...
if (delimiterInFile.length > 0) { delimiter = delimiterInFile[0]; }
// Catch IO Exceptions
try {
//Place the contents of file into a ArrayList
List<String> list = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Get the greatest number of delimited columns contiained
// within the ArrayList the whole while ignoring blank lines
// (there could be blank lines in file). Our columns for our
// double 2D Array will be determined from this value. We also
// determine the true number of rows required (remember, no
// blank elements allowed so ArrayList.size() wont be good enough).
int r = 0, c = 0;
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
int l = list.get(i).split(delimiter).length;
if (l > c) { c = l; }
r++;
}
}
// If we have nothing then the get outta here
// by returning null.
if (r == 0 || c == 0) { return null; }
// Declare and initialize a double data type 2D Array
double[][] array = new double[r][c];
// Fill the double type array...
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals("")) {
String[] data = list.get(i).split(delimiter);
for (int j = 0; j < data.length; j++) {
array[i][j] = Double.parseDouble(data[j]);
}
}
}
return array;
} catch (IOException ex) {
// Do what you want with the Exception...
ex.printStackTrace();
return null;
}
}
This method will automatically determine the required number of Rows and Columns for the returned Double data type 2D Array. The method ignores blank file lines so the required Rows needed for the returned Double 2D Array is determined with this in mind. The number of Columns value is determined by iterating through the data lines and detecting which data line contains the most delimited data. That column count is used for the entire 2D Array. This means then that file data lines which contain less columns will have their remaining Array Elements filled with a 0.0 double type value.
The optional delimiter that can be passed to this method can be any string or a RegEx (Regular Expression) string, for example: " " or "\\s+" or "," or ", " or "\t", etc.
This method will also throw an IO Exception should there be a problem accessing the supplied data file.
With the data file schema you provided:
0.4658 0 3
0.4095 0 3
0.4594 0 3
0.4297 0 3
0.3963 0 3
0.4232 0 3
0.4633 0 3
0.5384 0 3
0.5042 0 3
0.4328 0 3
and let's assume this file is named data.txt which is in your classpath, you might use this method like this:
double[][] myData = matrix("data.txt", "\\s+");
for (int i = 0; i < myData.length; i++) {
String strg = "";
for (int j = 0; j < myData[0].length; j++) {
strg+= myData[i][j] + " ";
}
System.out.println(strg);
}
Keep in mind, this is not a method I would recommend for really large data files (Hundreds of thousands of lines).
I have a csv file that is formatted as follows:
1,,3,4
3,4,,0
2,,4,2
I'm reading that file into a String variable called text. Then, doing this
String newText = text.replaceAll("","0");
In my output, it's replacing the no value elements with a 0 which is what I want, but it's also appending a 0 before and after the elements that do have a value.
010
0
030
040
030
040
0
000
020
0
040
020
Any helps is appreciated !
EDIT: Here is the rest of my code for clairification.
import java.lang.*;
public class DataMatrix {
private double[][] dMatrix;
public DataMatrix(String text) {
String[] line = text.split("\n");
// Creates an array for the CSV file
String[][] sMatrix = new String[line.length][];
for(int i=0; i < line.length; i++)
sMatrix[i] = line[i].split(",");
/*for(int j=0; j < sMatrix.length; j++) {
for(int x=0; x <= line.length; x++) {
if(sMatrix[j][x] !=*/
System.out.println("Original String: ");
for(int x=0; x < sMatrix.length; x++) {
for(int j=0; j <= line.length; j++) {
System.out.println(sMatrix[x][j]);
}
}
dMatrix = new double[sMatrix.length][];
System.out.println("Converted to double: ");
for(int x=0; x < sMatrix.length; x++) {
dMatrix[x] = new double[sMatrix[x].length];
for(int j=0; j <= sMatrix.length; j++) {
dMatrix[x][j] = Double.parseDouble(sMatrix[x][j]);
System.out.println(dMatrix[x][j]);
}
}
}
public void avgRowValue() {
double tempTotal = 0;
for(int x=0; x < dMatrix.length; x++) {
System.out.println("***********************************");
System.out.println("The average for row "+(x+1)+" is: ");
for(int i=0; i < dMatrix[x].length;i++) {
tempTotal += dMatrix[x][i];
double rowAvg = tempTotal / dMatrix[x].length;
System.out.println(rowAvg);
tempTotal = 0;
}
}
}
public void avgColValue() {
double tempTotal = 0;
for(int x=0; x < dMatrix[0].length; x++) {
System.out.println("**************************************");
System.out.println("The average for column "+(x+1)+" is: ");
for(int i=0; i <= dMatrix.length-1; i++)
tempTotal += dMatrix[i][x];
double colAvg = tempTotal / dMatrix.length;
System.out.println(colAvg);
tempTotal = 0;
}
}
public void overallAvgValue() {
double tempTotal = 0;
int count = 0;
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
tempTotal += dMatrix[x][i];
count++;
}
}
double overallAvg = tempTotal / count;
System.out.println("The overall average for the data matrix is: "+overallAvg);
}
public void maxValue() {
double maxValue = 0;
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
if(dMatrix[x][i] >= maxValue)
maxValue = dMatrix[x][i];
}
}
System.out.println("The max value for the data matrix is: "+maxValue);
}
public void minValue() {
double minValue = dMatrix[0][0];
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
if(dMatrix[x][i] <= minValue)
minValue = dMatrix[x][i];
}
}
System.out.println("The min value for the data matrix is: "+minValue);
}
}
and my main:
import java.util.*;
public class Assignment1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Make sure the user passed the command line argument - and nothing else
if (args.length != 1){
System.out.println("Assignment1 takes exactly one command-line argument.");
System.out.println("Usage: java Assignment1 some_file.csv");
System.exit(0);
}
String csvFileName = args[0];
//Instantiate my custom file reader
CSVReader fileReader = new CSVReader();
//Read the file into a string
String text = fileReader.readFile(csvFileName);
String newText = text.replaceAll("", "0");
//Create a new instance of DataMatrix that takes in String CSV file and converts to double array
DataMatrix matrix = new DataMatrix(newText);
boolean done = false;
while(done != true) {
System.out.println("**********************");
System.out.println("CSV Reader Menu: ");
System.out.println("1. Display the average values of each individual row.");
System.out.println("2. Display the averages value of each individual column.");
System.out.println("3. Display the average of the entire data matrix.");
System.out.println("4. Display the maximum value of the entire data matrix.");
System.out.println("5. Display the minimum value of the entire data matrix.");
System.out.println("0. Exit.");
int choice = input.nextInt();
switch(choice) {
case 1: matrix.avgRowValue();
break;
case 2: matrix.avgColValue();
break;
case 3: matrix.overallAvgValue();
break;
case 4: matrix.maxValue();
break;
case 5: matrix.minValue();
break;
case 0: done = true;
break;
default: System.out.println("Invalid input.");
break;
}
}//end of menu loop
}//end of main
}//end of class
With text.replaceAll("", "0") you are looking for all the places where there is a zero-length substring in the string. This is, for any string, between any two characters as well as at the beginning and at the end of the string.
"12345".replaceAll("", "_") // _1_2_3_4_5_
If you really want to use regex for this, you could use replaceAll("^$", "0"), where ^ and $ are the beginning and the end of the string, respectively. Alternatively, just check whether the string is empty:
String newText = text.isEmpty() ? "0" : text;
Example:
for (String s : "1,,23,4".split(",")) {
System.out.println(s.isEmpty() ? "0" : s); // prints 1, 0, 23, and 4
System.out.println(s.replaceAll("^$", "0"));// prints 1, 0, 23, and 4
}
If you want to insert those 0 into the original string, i.e. before using split, you could use a disjunction of different regexes for the start-of-string, mid-string, and end-of-string cases, using lookahead and lookbehind assertions, e.g. (?<=^|,) for "preceded by start-of-string or ,", and (?=,|$) for "followed by , or end-of-string":
String nums = ",1,,23,4,";
System.out.println(nums.replaceAll("(?<=^|,)(?=,|$)", "0"));
// output: 0,1,0,23,4,0
Or you could use capturing groups and group references in the replacement string to carry the , over into the replacement. Here, $10$2 means "1st group, then 0, then 2nd group":
System.out.println(nums.replaceAll("(^|,)(,|$)", "$10$2"));
i prefer to do this while reading from csv file , its cost is much less than doing it after reading csv file , so i make a csvreader and an array named Line , reading line by line from csv , and if the length of a string is bigger than 0 it written in this mode : 0thatstring0 , otherwise it written as 0
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] Line;
while ((Line = reader.readNext()) != null) {
for(int i = 0 ; i < Line.length ; i++)
if(Line[i].length>0) System.out.println(Line[i]);
else System.out.println("0");
}
second method , i'm not offering it , is using split method and spliting each string with "," , do the replace method and it works
I suggest using a library such as OpenCSV to read that file, then replace the empty cells as you encounter them. It will be a lot more robust.
I understand that text contains the value of a single column, after you split the line based on commas. You want to change empty strings into "0".
The replaceAll() method takes a regex. An empty string is strange for a regex. What you want is text.replaceAll("^$", "0").
After a thorough survey of this and many other Internet communities I failed to solve my problem. It is about ControlP5 button and my aim of importing 2D array from previously formated text file with two columns and 19 rows and space separated values. Now my code works but the 2D arrray I designated to hold the values from the txt does not get all of the values but just the last pair in its first row. I know that for loops over all values but stores them only in the first row. I dont know how to push it in another row for reading. This is my code:
if(theEvent.isController())
{
if(theEvent.controller().name() == "mean shape males")
{
String loadPath1 = selectInput();
reader = createReader(loadPath1); //new BufferedReader
int x=0; //rows
int y=0; //columns
String smaLine;
try
{
while ((smaLine = reader.readLine()) != null)
{
String[] values = smaLine.split(", ");
for(int i = 0; i < values.length; i++)
{
float[] testC = float(split(values[i], " "));
for (int j = 0; j < testC.length; j++)
{
mat1[j][i] = testC[j]; //THIS IS THE PROBLEMATIC MATRIX
}
}
x = x+1;
}
}
catch (IOException e)
{
e.printStackTrace();
}
mat1max = maxRet(mat1);
mat1min = minRet(mat1);
inputMat = new Grid(2,19,10,130,22,mat1,mat1min,mat1max);
}
}
I used all the advice I could find on Stack Overflow mainly from this post How to print 2D Array from .txt file in Java but I just can`t seem to move the reader onto rows other then the first.
I'm guessing that instead of resetting the elements in mat1 you want to create a new matrix for each line and store them in a list of some kind. This is how you might do it:
List<?> matrices = new ArrayList<?>();
while ((smaLine = reader.readLine()) != null)
{
float[][] mat = new float[MATRIX_ROWS][MATRIX_COLUMNS];
String[] values = smaLine.split(", ");
for(int i = 0; i < values.length; i++)
{
float[] testC = float(split(values[i], " "));
for (int j = 0; j < testC.length; j++)
{
mat[j][i] = testC[j];
}
}
matrices.add(mat);
x = x+1;
}
Where is x used, by the way?
Swap your mat1[j][i] = testC[j] to mat1[i][j] = testC[j]