Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. 50 3 21 10 9 9 54 47 24 74
  2. 22 63 63 28 36 47 60 3 45 83
  3. 20 37 11 41 47 89 9 98 40 94
  4. 48 77 93 68 8 19 81 67 80 64
  5. 41 73 24 29 99 6 41 23 23 44
  6. 43 41 29 11 43 94 62 27 81 71
  7. 83 14 97 67 21 68 77 25 21 24
  8. 31 8 54 14 49 96 33 18 14 80
  9. 54 55 53 38 62 53 62 10 42 29
  10. 17 89 92 87 15 42 50 85 68 43
  11.  
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.Scanner;
  15. public class Summer {
  16. public static void main(String args[]) throws IOException {
  17.  
  18. File text = new File("src/nums.txt");
  19.  
  20. if (!text.exists()) {
  21. text.createNewFile();
  22. }
  23.  
  24. int sum = 0;
  25. Scanner input = new Scanner(text);
  26. while (input.hasNextInt()) {
  27. sum = sum + input.nextInt();
  28.  
  29. }
  30.  
  31. System.out.printf("Sum of all numbers: %d", sum);
  32.  
  33.  
  34. int lineSum = 0;
  35. int lineNum = 1;
  36.  
  37. while (input.hasNext()) {
  38. if (input.hasNextInt()) {
  39. lineSum = lineSum + input.nextInt();
  40. } else {
  41. input.next();
  42. lineNum++;
  43. }
  44. }
  45.  
  46. System.out.printf("%nSum of line %d: %d", lineNum, lineSum);
  47. }
  48. }
  49.  
  50. Sum of all numbers: 4687
  51. Sum of line 1: 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement