Read two matrices from file
Hello to everybody. I ve got a txt file, for instance 4x4 and i want to two matrices 4x2. I know how to read variables from a file. Have you any idea how can I do it for the matrices??
My code is this:
Code:
public void OpenFileRead(String fileName) { //anoigma tou arxeiou gia diavasma
try {
input = new Scanner(new File(fileName));
} catch (FileNotFoundException e) { //sfalma kata tin evresi kai to anoigma tou arxeiou
System.err.println("Σφάλμα κατά το άνοιγμα αρχείου");
System.exit(0); //eksodos
}
}
public void Load() { //anagnwsi dedomenwn apo arxeio
try {
while (input.hasNext()) { //oso tha iparxei apothikeumeni eggrafi
String s1 = new Integer(input.nextInt()).toString();
int v = Integer.parseInt(s1.trim()); //pairnei to string kai metafrazei to antistoixo integer
String s2 = new Integer(input.nextInt()).toString();
int s = Integer.parseInt(s2.trim()); //pairnei to string kai metafrazei to antistoixo Long
System.out.println(v);
}
} catch (NoSuchElementException e) {
System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου");
System.err.println(e.getMessage());
input.close();
System.exit(0);
} catch (IllegalStateException e) {
System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο");
System.exit(0);
}
I have define everything that i ve to.
[SOLVED]Read two matrices from file
I dont if I must begin a new thread for this. I ve tried to use arraylist to store the data from file. I want a two dimensional array so i used an arraylist of arraylist. In the function Load that I made i ve defined this arraylist instead of the two dimensional matrix.
The code is this:
Code:
public void Load() { //anagnwsi dedomenwn apo arxeio
double[][] w1=new double[50][9];
int a=w1.length;
ArrayList<ArrayList<Double>> w2 = new ArrayList<ArrayList<Double>>();
try {
while (input.hasNext()) { //oso tha iparxei apothikeumeni eggrafi
for (int i= 0; i < 50; i++){
for (int j= 0; j < 9; j++){
w1[i][j]= input.nextDouble();
double d=input.nextInt();
w2.add(new ArrayList<Double>());
w2.add(new Double(d));
w2=input.nextDouble();
// System.out.print(w1[i][j]+" ");
System.out.print(w2);
}
System.out.println();
}
}
} catch (NoSuchElementException e) {
System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου");
System.err.println(e.getMessage()); //emfanisi tou minimatos sfalmatos
input.close();
System.exit(0);
} catch (IllegalStateException e) {
System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο");
System.exit(0);
}
}
w1 works properly and thanks again for your help. When i tried to use w2 i got this message:
cannot find symbol
symbol:method add(java.long.Double)
location class java.util.ArrayList<java.util.ArrayList<java.lang. Double>>