Guest User

Untitled

a guest
May 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. /**
  2.      * Reads data from given text file, instantiates objects
  3.      * (i.e. students and their marks) and inserts them into the teachers notebook.
  4.      *
  5.      * @param file Text file to read from
  6.      * @throws FileNotFoundException if given file cannot be opened for reading
  7.      * @throws IOException on any other I/O failure
  8.      */
  9.     public void load(File file) throws FileNotFoundException, IOException {
  10.         if (file == null) {
  11.             throw new IOException("File is null ");
  12.         }
  13.         if (!file.canRead()) {
  14.             throw new FileNotFoundException("File can't be read");
  15.         }
  16.        
  17.         BufferedReader br = null;
  18.        
  19.         try {
  20.             br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));            
  21.        
  22.             String line = br.readLine();
  23.             while(line != null) {
  24.                 String[] split = line.split(":");
  25.                 if (split.length != 2) {
  26.                     throw new IOException("Wrong input format ");
  27.                 }
  28.            
  29.                 String[] studentSplit = split[0].split(" ");
  30.                 if (studentSplit.length != 3) {
  31.                     throw new IOException("Wrong input format ");
  32.                 }
  33.                 int uco = 0;
  34.                 try {
  35.                     uco = Integer.parseInt(studentSplit[0]);
  36.                 }
  37.                 catch (NumberFormatException ex) {
  38.                     throw new IOException("Wrong input format ");
  39.                 }
  40.                 if (uco == 0) {
  41.                     throw new IOException("Wrong input format ");
  42.                 }
  43.                 Student student = new Student(uco, studentSplit[1] + " " + studentSplit[2]);
  44.            
  45.                 List<Mark> marks = new ArrayList<Mark>();
  46.                 String[] markSplit = split[1].split(" ");
  47.                 for (int i = 0; i < markSplit.length; i++) {
  48.                     try {
  49.                         marks.add(new Mark(Integer.parseInt(markSplit[i])));
  50.                     }
  51.                     catch (NumberFormatException ex) {
  52.                         throw new IOException("Wrong input format ");
  53.                     }
  54.                 }
  55.            
  56.                 notebook.put(student, marks);
  57.                 line = br.readLine();
  58.             }
  59.         } catch (IOException ex) {
  60.             throw new IOException(ex);
  61.         } finally {
  62.             br.close();
  63.         }
  64.     }
Add Comment
Please, Sign In to add comment