title: A Recursion method that returns number of digits, help for 0
body: <p>So I'm doing this exercise and my teacher says that he wants the num/10 in this line:<code>System.out.print(numDigits(num/10));</code> in the method (so that if the original number is 0 then it will return 1 digit rather than 0), but I have no idea where he wants it.</p>
  
  <p>(*must be done with recursion to count the number of digits of a number)</p>
  
  <p>To me this way is simple, but I guess if you're writing a lot of code you don't want to keep typing x/10.</p>
  
  <pre><code>import java.util.*;
  
  public class NumDigits
  {
    public static void main(String[]args)
    {
      int num;
  
      Scanner input = new Scanner(System.in);
      num = input.nextInt();
      input.close();
  
      System.out.print(numDigits(num/10));
    }
  
    public static int numDigits(int x)
    {  
      if (x == 0)
      {return 1;}
      else
      {
        return 1 + numDigits(x/10);
      }
    }
  }
  </code></pre>
  
  <p>I guess I could do this, but it takes more code... : / </p>
  
  <pre><code>import java.util.*;
  
  public class NumDigits
  {
    public static void main(String[]args)
    {
      int num;
  
      Scanner input = new Scanner(System.in);
      num = input.nextInt();
      input.close();
  
      System.out.print(numDigits(num, num));
    }
  
    public static int numDigits(int x, int num)
    {  
      if (num == 0)
      {return 1;}
      else if (x == 0)
      {return 0;}
      else
      {
        return 1 + numDigits(x/10, num);
      }
    }
  }
  </code></pre>