title: Extracting even digits from int
body: <p>Here is the <a href="http://practiceit.cs.washington.edu/problem.jsp?category=Building%20Java%20Programs%2C%203rd%20edition%2FBJP3%20Chapter%2012&amp;problem=bjp3-12-e14-evenDigits" rel="nofollow">problem</a> that I am solving.</p>
  
  <blockquote>
    <p>Write a method evenDigits that accepts an integer parameter n and that
    returns the integer formed by removing the odd digits from n. The
    following table shows several calls and their expected return values:</p>
  </blockquote>
  
  <pre><code>Call    Valued Returned
  evenDigits(8342116);    8426
  evenDigits(4109);   40
  evenDigits(8);  8
  evenDigits(-34512); -42
  evenDigits(-163505);    -60
  evenDigits(3052);   2
  evenDigits(7010496);    46
  evenDigits(35179);  0
  evenDigits(5307);   0
  evenDigits(7);  0
  </code></pre>
  
  <p>If a negative number with even digits other than 0 is passed to the method, the result should also be negative, as shown above when -34512 is passed.</p>
  
  <p>Leading zeros in the result should be ignored and if there are no even digits other than 0 in the number, the method should return 0, as shown in the last three outputs.</p>
  
  <p>I have this so far - </p>
  
  <pre><code>public static int evenDigits(int n) {
      if (n != 0) { 
          int new_x = 0;
          int temp = 0;
          String subS = "";
      String x_str = Integer.toString(n);
          if (x_str.substring(0, 1).equals("-")) {
               temp = Integer.parseInt(x_str.substring(0, 2));
               subS = x_str.substring(2);
          } else {
               temp = Integer.parseInt(x_str.substring(0, 1));
               subS = x_str.substring(1);
          }
  
          if (subS.length() != 0) {
               new_x = Integer.parseInt(x_str.substring(1));
          }
          
          if (temp % 2 == 0) {
               return Integer.parseInt((Integer.toString(temp) + evenDigits(new_x)));
          } else {
              return evenDigits(new_x);
          }
      }
      return 0;
  }
  </code></pre>