Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class _3_BePositive {
- @SuppressWarnings({"resource"})
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- int countSequences = scn.nextInt();
- //Make for loop <= so to not skip last sequence
- for (int i = 0; i <= countSequences; i++) {
- //Added string to count the lines
- String lineCount = scn.nextLine();
- String[] input = lineCount.trim().split(" ");
- //Keeping line count in if statement to avoid empty first line
- if (lineCount.equals("")) {
- continue;
- }
- ArrayList<Integer> numbers = new ArrayList<>();
- for (int j = 0; j < input.length; j++) {
- if (!input[j].equals("") ) {
- //Parse input[j] not input[i] like it was before
- int num = Integer.parseInt(input[j]);
- numbers.add(num);
- }
- }
- boolean found = false;
- for (int j = 0; j < numbers.size(); j++) {
- int currentNum = numbers.get(j);
- //we have to print numbers equal to zero so we make it ">="
- if (currentNum >= 0) {
- // changing "==" to "!=" to make it truly
- System.out.printf("%d%s", currentNum, j != numbers.size() - 1 ? " " : "\n");
- found = true;
- } else {
- //check if index is last, if its last it continues to other sequence otherwise index out of bound exception
- if (j >= numbers.size() - 1) {
- continue;
- }
- currentNum += numbers.get(j + 1);
- //making statement ">=" instead of ">" to print numbers equal to zero
- if (currentNum >= 0) {
- // changing "==" to "!=" in previous statement
- System.out.printf("%d%s", currentNum, j != numbers.size() - 1 ? " " : "\n");
- found = true;
- //if it founds number to addition we go to next number by increment index
- j++;
- } else {
- //increment index
- j++;
- }
- }
- }
- if (!found) {
- System.out.println("(empty)");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement