Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //added package InstructionSet
- package InstructionSet;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class BePositive_Broken {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- Scanner scn = new Scanner(System.in);
- int countSequences = scn.nextInt();
- // make sure that cursor goes to new line
- scn.nextLine();
- for (int i = 0; i < countSequences; i++) {
- //added regex expression to divide by multiple spaces
- String[] input = scn.nextLine().split("\\s+");
- ArrayList<Integer> numbers = new ArrayList<>();
- for (int j = 0; j < input.length; j++) {
- if (!input[j].equals("")) {
- // inside inner loop, use j as counter but also trim
- int num = Integer.parseInt(input[j].trim());
- numbers.add(num);
- }
- }
- boolean found = false;
- for (int j = 0; j < numbers.size(); j++) {
- int currentNum = numbers.get(j);
- // check if zero
- if (currentNum >= 0) {
- //reversed conditions on ternary expression
- System.out.printf("%d%s", currentNum,j == numbers.size() - 1 ? "\n" : " ");
- found = true;
- } else {
- currentNum += numbers.get(j + 1);
- // move to next number
- j++;
- // check if zero as well
- if (currentNum >= 0) {
- //reversed conditions on ternary expression
- System.out.printf("%d%s", currentNum,j == numbers.size() - 1 ? "\n" : " ");
- found = true;
- }
- }
- }
- if (!found) {
- System.out.println("(empty)");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement