Hello,
I have a file and this how it looks like:
6
{a, b, c, d, e, f}
1 1 1 0 0 0
0 1 1 0 0 0
0 0 1 0 0 0
0 0 1 1 0 0
0 1 1 1 1 0
1 1 1 1 1 1
6 is the number of elements in the set and {a, b, c, d, e, f} is the list of set members and the matrix is the relation matrix.
I'm trying to read the file and store the matrix in 2-D array but when I print the matrix it prints zeros but with the correct size ! and this is my whole code:
import java.util.*;
import java.io.*;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
public class Test1 {
public static void main(String[] args) {
Scanner key= new Scanner (System.in);
System.out.println("Enter the name of the file: ");
String name=key.next();
Scanner inputStream=null;
try{
inputStream= new Scanner(new FileInputStream(name));
}
catch(FileNotFoundException e)
{
System.out.println("File"+name+" is not found!");
System.out.println("or couldn't be opened!");
System.exit(0);
}
int size=0;
if (inputStream.hasNext()){
size= inputStream.nextInt();
inputStream.nextLine();
}
else{
System.out.println("The Size of the matrix is not clearly stated!");
}
int array[][]=new int[size][size];
while (inputStream.hasNextInt()){
for (int i=0;i<array.length;i++){
for (int j=0;j<array[i].length;j++){
array[i][j]=inputStream.nextInt();
inputStream.next();
}
}
}
inputStream.close();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <array[i].length; j++){
System.out.print(array[i][j]);
System.out.print(" ");
} System.out.println();
}
}
}
Could any one please tell me why the output is zeros but the size of the matrix is correct ! and where is exactly the wrong in my code ?
Thanks in advance :)