0

this is home work and I am having difficulty with this problem. Here is the entire problem.

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.

This is what I have so far

 public int countPos(Scanner sc)
 {
  if(sc.hasNextInt())
   {
    int val=sc.nextInt();
    if (val>0)
    {
    return countPos(sc,val)+val;
    }
    else
    {
       return 0;
    }


  }
}

The homework is online and works by submitting the method and checking to see if it works and accepts it. I am beginner learner java and just started recursion and do not really have an understanding of it. Any help solving this answer would be greatly appreciated. Thank you.

3 Answers3

0

Ok, you're on the right track.

First things first. When you create a recursive function, the first thing to implement is your recursion termination (when your recursive function should stop being called). In your code, it stops either when you get a negative int (not what you want), or when there's no more int to be read (this is what you want).

Second thing, you're calling your countPos method with two parameters, the Scanner instance and your current count. But your function only accepts one parameter so your code won't compile.

0

On recursive functions you have to decide the recursion termination. The termination that I have used is may not be the one you need, because it stops at the first non-integer digit.

At your code your adding val at functions return, which is the value of the integer you have read, not another integer.

The following implementation is not a "tail recursion". It is a good idea to look at this example What is tail recursion? and implement tail recursion for your solution.

import java.io.*;
import java.util.Scanner;


class HelloWorldApp {
public static int countPos(Scanner sc)
 {
  if(sc.hasNextInt())
   {
    int val = sc.nextInt();
    if (val > 0)
    {
        return countPos(sc) + 1;
    }
    else if (val =< 0)
    {
       return countPos(sc);
    }
    else {
        return 0;
    }
  }

  return 0;
}
    public static void main(String[] args) {
        System.out.println("--> " + countPos(new Scanner(System.in)));
    }
}
Community
  • 1
  • 1
Angelos
  • 533
  • 6
  • 19
  • Yes, thank you it works. Just on the else if statement the =< has to be switched. I am having a a hard time just trying to find out where to terminate my recursion, in similar problems – Daniel Johnson Mar 06 '17 at 20:43
0

This is the code I used for my online homework and I got it correct.

public int countPos(Scanner input)
{
    if(input.hasNextInt())
    {
        int x = input.nextInt();

        if(x > 0)
            return countPos(input)+ 1;
        else
            return countPos(input);
    }

        return 0;
}

`

Juli
  • 1