Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tasks_03_Loops;
- import java.util.Scanner;
- public class Hw_03_08_WordOrNumber_2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- int sum = 0;
- StringBuilder sb = new StringBuilder();
- while (num > 0) {
- String input = scanner.nextLine();
- if ((input.charAt(0) >= '0') && (input.charAt(0) <= '9')) {
- int currentInt = Integer.parseInt(input);
- sum +=currentInt;
- } else {
- if (sb.isEmpty()){
- sb.append(input);
- } else {
- sb.append('-').append(input);
- }
- }
- num--;
- }
- if (sb.isEmpty()) {
- System.out.println("no words");
- } else {
- System.out.println(sb.toString());
- }
- System.out.printf("%d%n", sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement