Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class SumAbcFile {
- public static void main(String[] args) throws IOException {
- try {
- Scanner in = new Scanner(new File(args[0]));
- PrintWriter writer = new PrintWriter(new File(args[1]));
- int number = 0, sum = 0;
- boolean non_positive = false;
- while (in.hasNextLine()) {
- String cur_line = in.nextLine();
- cur_line = cur_line.toLowerCase();
- String[] a = cur_line.split("[-a-j]");
- for (int i = 0; i < a.length; i++) {
- for (int k = 0; k < a[i].length(); k++) {
- if (a[i].charAt(k) - 'a' < 10 && a[i].charAt(k) - 'a' > 0) {
- number = number * 10 + a[i].charAt(k) - 'a';
- } else if (k == 0 && a[i].charAt(k) == '-') {
- non_positive = true;
- } else {
- throw new NumberFormatException();
- }
- }
- if (non_positive) {
- sum = sum - number;
- } else {
- sum = sum + number;
- }
- non_positive = false;
- }
- }
- writer.print(sum);
- } catch (FileNotFoundException e) {
- System.out.println("Error: Required file not found");
- } catch (IndexOutOfBoundsException e) {
- System.out.println("Please, enter a both names of files");
- } catch (IllegalStateException e) {
- System.out.println("IllegalStateException");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement