Advertisement
Guest User

Be Positive

a guest
Jan 26th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. //added package InstructionSet
  2. package InstructionSet;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class BePositive_Broken {
  8.     public static void main(String[] args) {
  9.         @SuppressWarnings("resource")
  10.         Scanner scn = new Scanner(System.in);
  11.  
  12.         int countSequences = scn.nextInt();
  13.         // make sure that cursor goes to new line
  14.         scn.nextLine();
  15.  
  16.         for (int i = 0; i < countSequences; i++) {
  17.             //added regex expression to divide by multiple spaces
  18.             String[] input = scn.nextLine().split("\\s+");
  19.             ArrayList<Integer> numbers = new ArrayList<>();
  20.  
  21.             for (int j = 0; j < input.length; j++) {
  22.                 if (!input[j].equals("")) {
  23.                     // inside inner loop, use j as counter but also trim
  24.                     int num = Integer.parseInt(input[j].trim());
  25.                     numbers.add(num);
  26.                 }
  27.             }
  28.  
  29.             boolean found = false;
  30.  
  31.             for (int j = 0; j < numbers.size(); j++) {
  32.                 int currentNum = numbers.get(j);
  33.  
  34.                 // check if zero
  35.                 if (currentNum >= 0) {
  36.                     //reversed conditions on ternary expression
  37.                     System.out.printf("%d%s", currentNum,j == numbers.size() - 1 ? "\n" : " ");
  38.                     found = true;
  39.                 } else {
  40.                     currentNum += numbers.get(j + 1);
  41.                     // move to next number
  42.                     j++;
  43.                     // check if zero as well
  44.                     if (currentNum >= 0) {
  45.                         //reversed conditions on ternary expression
  46.                         System.out.printf("%d%s", currentNum,j == numbers.size() - 1 ? "\n" : " ");
  47.                         found = true;
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             if (!found) {
  53.                 System.out.println("(empty)");
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement