Number frequency or number of 9's between 1 & 1000 - Java
<p>I came across this little question at work and wanted to see if I could write a little java program to solve it. Apparently I can't write a damn thing in java. Lol I am stuck but have part of it worked out. I spend the better part of a two days trying to get it working an have had no luck. Here is my problem. I have a txt file with the numbers generated in order 1-1000 one number per line. I want to calculate how many times or the frequency of the number nine between 1 &amp; 1000. My approach is as follows:</p>

<ol>
<li>Read the numbers from the text file and stick them in an int array. </li>
<li>Convert the working number or the number in spot[0] in the array to a string 
Pick one route. </li>
</ol>

<p>3-A. Compare with charAt(0) to a nine and increment if appropriate </p>

<p>3-B. Convert the string to a char array and compare index 0 in the char Array to a nine and increment if appropriate. </p>

<p>Side Note: You must comment out the else if comparing charArray[1] == '9' for it to compile. If you comment that out it will work perfectly for counting the nines in the first digit location. The second and third digit comparisons don't work. Thanks and sorry for the dumb question. </p>

<p>An easy way to generate the text file is to use excel type in a column 1 then 2 in the second row and then 3 in the third row (same cell) and then highlight all three numbers and drag the plus sign in the bottom right corner of the cell containing the three. This will fill down to 1000. I am not attaching my txt file since you can easily generate your own.</p>

<p>**Not homework  </p>

<p>My code so far is as follows: </p>

<pre><code> package com.numbers;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Numbers 
{

 static int[] array = new int[1002];
 static char[] charArray = new char[4]; 

public static void main(String[] args) throws FileNotFoundException
{

    Scanner scan = new Scanner (System.in);
    System.out.print("Enter the name of the file : ");
    String whatfile = scan.nextLine();
    Scanner readnumber = new Scanner (new FileReader(whatfile));

    int firstDigit  =  0;
    int secondDigit =  0; 
    int thirdDigit  =  0;

    for (int i = 0; i &lt; 1000; i++)
    {
            array[i] = readnumber.nextInt();                
            String testString = ""; 

    // System.out.println("the number being passed to parse is : " +    numberAtIndex); 


        // Approach 1 
        testString = Integer.toString(array[i]);

        if(testString.charAt(0) == '9')
        {
            firstDigit = firstDigit + 1; 
        }

// System.out.println("test string contains : " + testString);       

        // Approach 2 
        charArray =  testString.toCharArray();

        if(charArray[0] == '9')
        {
            firstDigit = firstDigit + 1; 

        }

    /*
     * If you comment out this 'else if' it will work. please comment out one of the approaches above (use only 1) My problem is that
     * I can't seem to figure out how to search for nines in the second &amp; third digit location. 
     * If it is in an array shouldn't I be able to compare the charArray[1] to a nine and have it increment if true?
     */

else if (charArray[1] == '9')
    {
        secondDigit = secondDigit + 1; 
    }


        } //  end for 

 // System.out.println("this is what is stored in array spot 0 : " + array[0]);     //should      be 1

 System.out.println("the number of 9's in the first digit place holder is : " + firstDigit);
System.out.println("the number of 9's in the second digit place holder is : " +   secondDigit);
// System.out.println("the number of 9's in the third digit place holder is : " + thirdDigit);

} // End of main 

 } // end of class
</code></pre>
