title: How to find the most frequently repeated number from the command line, in an array?
body: <p>I need to take single digit integers from the command line and put them into an array, and then find the most frequent integer. Sometimes this program seems to work, and other times, it doesn't.</p>
  
  <pre><code>public class MostFrequent {
      public static void main(String[] args){
      int num=0;
      int[] freq= new int[9];//intialize array for integers 0-9
      for (int i=0; i&lt;args.length; i++){
        try {
          num = Integer.parseInt(args[i]);
          freq[num]++;//adds to array counter
          }
          catch (NumberFormatException nfe) {
          }
      }
      int max=0,j;
      for (j=0; j&lt;10; j++){
        while(freq[j]&gt;max){
          max=freq[j];
        }
      }
      System.out.println("The digit that appears most frequently is " + freq[j]);
    }
  }
  </code></pre>
  
  <p>Thanks everyone for your help, this is what ended up working for me, and thanks to whoever mentioned making the array more dynamic, that helped as well. <strong>Here is the code I finished with:</strong></p>
  
  <pre><code>    public class MostFrequent {
  public static void main(String[] args){
  int num=0;
  int[] freq= new int[args.length];//intialize array for integers 0-9
  for (int i=0; i&lt;args.length; i++){
    try {
      num = Integer.parseInt(args[i]);
      freq[num]++;//adds to array counter
      }
      catch (NumberFormatException nfe) {
      }
  }
  int max=0,j;
  for (j=1; j&lt;args.length; j++){
    while(freq[j]&gt;freq[max]){//compares a max array val to the previous val
      max=j;
    }
  }
  System.out.println("The digit that appears most frequently is " + max);
  </code></pre>
  
  <p>}
  }</p>