Package ru.ifmo.ctddev.tsarev.task1

Source Code of ru.ifmo.ctddev.tsarev.task1.FileCalc

package ru.ifmo.ctddev.tsarev.task1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileCalc {
  private List<Function> functions;

  private void run(String[] args) {
    if (args.length != 2) {
      System.out.print(args.length
          + " aguments recived, but two expected");
      return;
    }

    File input = new File(args[0]);
    File output = new File(args[1]);
    try {
      Scanner sc = new Scanner(input);
      functions = new ArrayList<Function>();
      try {
        int count = 0;
        while (sc.hasNextLine()) {
          try {
            functions.add(new Function(sc));
          } catch (CalcException e) {
            System.out.println(e.getMessage() + " in expression #"
                + (count + 1));
            functions.add(new Function());
          }
          ++count;
        }
      } finally {
        if (sc.ioException() != null) {
          System.out.println("Error when trying to read from file \"" + args[0] + "\"");
          return;
        }
        sc.close();
      }
    } catch (FileNotFoundException e) {
      System.out.println("Can't read from file \"" + args[0] + "\"");
      return;
    }

    try {
      OutputGenerator.printTable(output, functions);
      System.out.println("SUCCESFUL. Look at the file \""
          + output.getName() + "\" to results");
    } catch (FileNotFoundException e) {
      System.out.println("Can't write to file \"" + args[0] + "\"");
      return;
    } catch (CalcIOException e) {
      System.out.println("Error when trying to write to file \"" + args[0] + "\"");
      return;
    }
  }

 

  public static void main(String[] args) {
    new FileCalc().run(args);
  }

}
TOP

Related Classes of ru.ifmo.ctddev.tsarev.task1.FileCalc

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.