Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Java to program the following. 1. Create public java class named Benford. 2.

ID: 3782948 • Letter: U

Question

Use Java to program the following.

1. Create public java class named Benford.

2. It's main function should prompt for the name of a data file and then the function iterates through the all of the elements in the data file and computes the frequency (count and percentage) of the first number in each entry.

3. Note: Some of the data files have comments that you must filter out.

4. Also note: that blank lines or lines where the number begins or is 0 must also be filtered out.

5. Your output must be formatted as follows.

Explanation / Answer

Hi Firend, Please find my code.

Please let me know in case of any issue.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class CountFrequency {

  

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

      

       Scanner sc = new Scanner(System.in);

      

       // taking input file name

       System.out.print("Enter input file name: ");

       String fileName = sc.next();

      

       // opening input file

       Scanner inputFile = new Scanner(new File(fileName));

      

       // declaring array to store count of first digit of each entery

       int[] frequency = new int[10];

       int totalCount = 0; // to maintain total count

      

       int num;

      

       // reading all lines

       while(inputFile.hasNextLine()){

           // reading current line

           String line = inputFile.nextLine();

           // if line is not empty

           if(line == null || line.isEmpty() || line.trim() == "")

               continue;

           // taking first character of current line

           char firstDigit = line.charAt(0);

          

           // if first character is digit and not equal to 0

           if(Character.isDigit(firstDigit) && firstDigit != '0'){

               // converting first character of current line in integer

               int d = firstDigit - '0';

               frequency[d]++;

               totalCount++;

           }

       }

      

       System.out.println("Digit Count Frequency");

       for(int i=1; i<=9; i++){

           System.out.println(i+" "+frequency[i]+" "+ ((double)frequency[i]/(double)totalCount*100));

       }

      

      

   }

}

/*

Sample run:

##### Entry of input.txt:

453 43

//543

123

0

4ee

875

52

// Output

Enter input file name: input.txt

Digit   Count   Frequency

1   1   20.0

2   0   0.0

3   0   0.0

4   2   40.0

5   1   20.0

6   0   0.0

7   0   0.0

8   1   20.0

9   0   0.0

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote