Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class SumAbcFile {
  4. public static void main(String[] args) throws IOException {
  5. try {
  6. Scanner in = new Scanner(new File(args[0]));
  7. PrintWriter writer = new PrintWriter(new File(args[1]));
  8. int number = 0, sum = 0;
  9. boolean non_positive = false;
  10. while (in.hasNextLine()) {
  11. String cur_line = in.nextLine();
  12. cur_line = cur_line.toLowerCase();
  13. String[] a = cur_line.split("[-a-j]");
  14. for (int i = 0; i < a.length; i++) {
  15. for (int k = 0; k < a[i].length(); k++) {
  16. if (a[i].charAt(k) - 'a' < 10 && a[i].charAt(k) - 'a' > 0) {
  17. number = number * 10 + a[i].charAt(k) - 'a';
  18. } else if (k == 0 && a[i].charAt(k) == '-') {
  19. non_positive = true;
  20. } else {
  21. throw new NumberFormatException();
  22. }
  23. }
  24. if (non_positive) {
  25. sum = sum - number;
  26. } else {
  27. sum = sum + number;
  28. }
  29. non_positive = false;
  30. }
  31. }
  32. writer.print(sum);
  33. } catch (FileNotFoundException e) {
  34. System.out.println("Error: Required file not found");
  35. } catch (IndexOutOfBoundsException e) {
  36. System.out.println("Please, enter a both names of files");
  37. } catch (IllegalStateException e) {
  38. System.out.println("IllegalStateException");
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement