diff --git a/tp2/Crossword.java b/tp2/Crossword.java index 110a866886a6190d4c3263eb23e08a2a05d31abd..94667058564dd1c9d273259bc19e947e0abfdd3a 100644 --- a/tp2/Crossword.java +++ b/tp2/Crossword.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.Scanner; import java.io.*; @@ -22,12 +23,34 @@ public class Crossword ** Updates <rows> and <columns> to height and width of the array. */ public Crossword(File file) throws IOException { + if(!file.exists()){ + System.out.println("le fichier n'existe pas"); + return; + } // Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns" - + + Scanner scan = new Scanner(file); + + columns = scan.next().length(); + while (scan.hasNextLine()) { + rows++; + scan.nextLine(); + } + // Code pour créer le tableau "array" - - // Code pour remplir "array" avec les caractères du Fichier "file" - + char[][] array = new char[rows][columns]; + + // Code pour remplir "array" avec les caractères du Fichier "file" + Scanner scanner = new Scanner(file); + for (int i = 0; i < rows; i++) { + String fileLine = scanner.nextLine(); + for (int j = 0; j < columns; j++) { + char nextChar = fileLine.charAt(j); + array[i][j] = nextChar; + } + } + this.array = array; + }