Advertisement
Guest User

Homework-Lab-3.BePositive

a guest
Jan 27th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class _3_BePositive {
  5.     @SuppressWarnings({"resource"})
  6.     public static void main(String[] args) {
  7.         Scanner scn = new Scanner(System.in);
  8.        
  9.         int countSequences = scn.nextInt();
  10.        
  11.         //Make for loop <= so to not skip last sequence
  12.         for (int i = 0; i <= countSequences; i++) {
  13.             //Added string to count the lines
  14.             String lineCount = scn.nextLine();
  15.             String[] input = lineCount.trim().split(" ");
  16.             //Keeping line count in if statement to avoid empty first line
  17.             if (lineCount.equals("")) {
  18.                 continue;
  19.             }
  20.             ArrayList<Integer> numbers = new ArrayList<>();
  21.            
  22.             for (int j = 0; j < input.length; j++) {
  23.                 if (!input[j].equals("") ) {
  24.                     //Parse input[j] not input[i] like it was before
  25.                     int num = Integer.parseInt(input[j]);
  26.                     numbers.add(num);  
  27.                 }                          
  28.             }
  29.            
  30.             boolean found = false;
  31.            
  32.             for (int j = 0; j < numbers.size(); j++) {             
  33.                 int currentNum = numbers.get(j);
  34.                 //we have to print numbers equal to zero so we make it ">="
  35.                 if (currentNum >= 0) {
  36.                     // changing "==" to "!=" to make it truly
  37.                     System.out.printf("%d%s", currentNum, j != numbers.size() - 1 ? " " : "\n");
  38.                     found = true;
  39.                 } else {
  40.                     //check if index is last, if its last it continues to other sequence otherwise index out of bound exception
  41.                     if (j >= numbers.size() - 1) {
  42.                           continue;
  43.                         }
  44.                     currentNum += numbers.get(j + 1);                  
  45.                     //making statement ">=" instead of ">" to print numbers equal to zero
  46.                     if (currentNum >= 0) {
  47.                         // changing "==" to "!=" in previous statement
  48.                         System.out.printf("%d%s", currentNum, j != numbers.size() - 1 ? " " : "\n");
  49.                         found = true;
  50.                         //if it founds number to addition we go to next number by increment index
  51.                         j++;
  52.                     } else {
  53.                         //increment index
  54.                         j++;
  55.                     }
  56.                 }
  57.             }
  58.             if (!found) {
  59.                 System.out.println("(empty)");
  60.             }          
  61.         }      
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement