Printing every digit of a number Which of these three methods is better?

import java.util.* ;    
public class anycode{

  public static void print_digits3(int num){
    //this method converts integer to string and parses it
    int digit = 0 ;
    String num_str = new Integer(num).toString() ;
    int counter = 0  ;
    while(counter != num_str.length()){
       System.out.println("digit: " + num_str.charAt(counter)  + "| real integer digit: " + Integer.parseInt(num_str.substring(counter,counter+1))) ;
      counter++ ;
    }

  }

  public static void print_digits4(int num){
    //this method converts integer to array of chars and parses it
     char[]num_sequence_of_chars = new Integer(num).toString().toCharArray() ;
     for(int i = 0 ; i &lt; num_sequence_of_chars.length ; i++){
       System.out.println("digit :" + num_sequence_of_chars[i] ) ;    
     }
  }
  public static void print_digits2(int num){
    //this is the MATH method of parsing number - take every first digit div by
    // 1000,100,10,1 and update num - num% to get the rest

    //it s clunky, hard to read
    int digit = 0 ;

    Integer res_int = new Integer(num) ;

    int num_len = res_int.toString().length();// need to know if the number is 3-digit,4-digit etc
    //so we can know the power of 10 - 10^3, 10^2, 10^1 etc
    System.out.println("len of num : " + num_len) ;
    num_len-- ;
    double base = 0 ;
    int temp ;
    while( num_len &gt;= 0){      
      temp = num ;
      base = Math.pow(10,num_len) ;      
      num = num / (int)base ;// type recasting to int is a must, it does not work without it - no idea why
      System.out.println("num :"  + num) ;
      num = (int) temp % (int)base ;      
      num_len-- ;          
  }

  }
  public static void print_digits(int num){
    int digit = 0 ;
    while(num != 0 ){
    digit = num%10 ;
    System.out.print(digit + "\t") ;
      num = num/ 10 ;      
    }
  }

  public static void main(String args[]){
              print_digits2(7658) ;
              System.out.println("--------------------------") ;
              print_digits3(7658) ;
  }

}


How can I rewrite the print_digits2?  I want a more efficient MATH method of doing - no converting to string \ char[].
To be frank, I don't really know why you need such methods to print out individual characters, I'll just make my suggestions based on what you have...


Your class name anycode should be in PascalCase.
Your method names should be in camelCase.
Eclipse tells me that the assignments int digit = 0; in print_digits3 and print_digits2 are not used.
You have this comment: // type recasting to int is a must, it does not work without it - no idea why - that's because when you divide an int with a double, the result will be double type but you are assigning that to an int value. Therefore, you are required to cast the double into an int first for the assignment to be correct.
Recommended to use Integer.valueOf() instead of new Integer().


Not sure what's your aversion to using the char[] way of doing the same task, but here's a suggested approach anyways for comparison (to your print_digits4 method):

private static void printDigits( int num ) {
    for ( char c : String.valueOf( num ).toCharArray() ) {
        System.out.println( c );
    }
}