Advertisement
ivoCod

Untitled

Aug 20th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. package Tasks_03_Loops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Hw_03_08_WordOrNumber_2 {
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int num = Integer.parseInt(scanner.nextLine());
  11. int sum = 0;
  12. StringBuilder sb = new StringBuilder();
  13.  
  14. while (num > 0) {
  15. String input = scanner.nextLine();
  16.  
  17. if ((input.charAt(0) >= '0') && (input.charAt(0) <= '9')) {
  18. int currentInt = Integer.parseInt(input);
  19. sum +=currentInt;
  20. } else {
  21. if (sb.isEmpty()){
  22. sb.append(input);
  23. } else {
  24. sb.append('-').append(input);
  25. }
  26. }
  27.  
  28. num--;
  29. }
  30.  
  31. if (sb.isEmpty()) {
  32. System.out.println("no words");
  33. } else {
  34. System.out.println(sb.toString());
  35. }
  36. System.out.printf("%d%n", sum);
  37.  
  38. }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement