Write the definition of a method named countPos that receives a reference to a Scanner object associated with a stream of input consisting of integers only.

1
Write the definition of a method named countPos that receives a reference  to a Scanner object associated with a stream of input consisting of integers only.

LANGUAGE: JAVA

CHALLENGE:

Write the definition of a method named countPos that receives a reference to a Scanner object associated with a stream of input consisting of integers only. The method reads all the integers remaining to be read in standard input and returns the number that are positive. So if the input were:
19 -5 -3 -251 14
-7 -14 6
the method would return 3 because there are 3 positive integers there.
The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

SOLUTION:

static int countPos(Scanner input){
    int count=0;
    int value;
    if(input.hasNextInt()){
        value=input.nextInt();
        count=countPos(input);
        if(value>0)
            count++;
    }
    else
    {
        return count;
    }
    return count;
}

One comment

Add comment