Reputation: 3
I'm trying to read this file:
1|2|3|4|5|81||
6|7|8|9|10|91||
11|12|13|14|15|92||
16|17|18|19|20|93||end
and store them as a 2D array with delimiter |
. This is my approach
public void setMatrixFromFile(FileReader fIn) throws FileNotFoundException{
int row, col;
Matrix = new double[HIGHEST_INDEX+1][HIGHEST_INDEX+1];
Scanner scanFile;
scanFile = new Scanner(fIn).useDelimiter("|");
row = LOWEST_INDEX + 1;
col = 0;
while (row <= HIGHEST_INDEX && (scanFile.hasNext()) &&
(!scanFile.next().matches("(.*)end(.*)"))){
col = LOWEST_INDEX + 1;
while (col <= HIGHEST_INDEX && scanFile.hasNext() &&
(!scanFile.next().matches("(.*)||(.*)"))){
if (scanFile.hasNextDouble() || scanFile.hasNextInt()){
this.setElMatrix(row, col, scanFile.nextDouble());
col++;
}
}
if (scanFile.hasNext()){
scanFile.next();
}
row++;
}
this.setColEff(col);
this.setRowEff(row-1);
}
setElMatrix
as it name suggests only has this
public void setElMatrix(int i, int j, double elMatrix)
{
Matrix[i][j] = elMatrix; // i = row index, j = column index
}
Well, HIGHEST_INDEX
has set to 100
and LOWEST_INDEX
has set to 0
. Then, I have following code to (manually) test the above code.
import java.util.*;
import java.io.*;
public class TestMatriks
{
public static void main (String []args) throws FileNotFoundException{
Matriks mTest1 = new Matriks();
FileReader fin = new FileReader("testcase.txt");
mTest1.setMatrixFromFile(fin);
mTest1.outMatrix();
}
}
outMatrix
is used to output the 2d array into monitor and expected output is
1.0 2.0 3.0 4.0 5.0 81.0
6.0 7.0 8.0 9.0 10.0 91.0
11.0 12.0 13.0 14.0 15.0 92.0
16.0 17.0 18.0 19.0 20.0 93.0
When the above code is executed, it produces this:
0.0
0.0
I still couldn't figure what's wrong with my code. Yes, I've read as many references as I could, asking my friends, but that couldn't help.
Second question: If I move the file into directory that not the same as the directory which I've put in the source code, how can I modify the above code? I want to put the whole project (and the file) into a portable storage, and put the input file into a separated folder from the code.
Edit:
More details on Matrix
class:
import java.util.*;
import java.io.*;
public class Matriks
{
private static final int ROW_SIZE = 101;
private static final int COLUMN_SIZE = 101;
private static final double Val_Undef = -999999999999999.999; //unused
private static final int LOWEST_INDEX = 0;
private static final int HIGHEST_INDEX =100;
private static final int IDX_UNDEF = -9999;
private double[][] Matrix;
private int rowEff;
private int colEff;
//methods
}
Sorry for my poor english, and sorry if my questions are too obvious. I'm still at beginner level in java.
Upvotes: 0
Views: 104
Reputation: 137
Few observations:
index should start with 0. below +1 is not required. row = LOWEST_INDEX + 1; col = LOWEST_INDEX + 1;
Since you tokenize using | delimiter, you will not be able to catch || in the below regex match. !scanFile.next().matches("(.)||(.)")
Put some System.out logs in middle of the code and see the data return by scanner.
Upvotes: 1