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: 3782698 • 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

package chegg;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class Benford {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the file name");
       String FILENAME;
       FILENAME=sc.nextLine();
       BufferedReader br = null;
       //FILENAME = "C:/Users/ViveK/Desktop/biz.txt";
       FileReader fr = null;

int freq[]=new int[10]; //array for storing frequency of all digits

for(int i=0; i<10; i++)
{
freq[i]=0; //intializing the count of every digit with '0'
}

       try {

           fr = new FileReader(FILENAME);
           br = new BufferedReader(fr);

           String line;

           br = new BufferedReader(new FileReader(FILENAME));
           boolean comment = false;
           System.out.println("Digit"+" "+"count"+" "+"Frequency");
          
           while ((line = br.readLine()) != null) {
              
               if (line.contains("/*")) {
                   comment = true;
                   continue;
               }
               if (line.contains("*/")) {
                   comment = false;
                   continue;
               }
               if (line.contains("//")) {
                   continue;
               }
               if (!comment) {  
                   String []arr=line.trim().split(" ");
                   for(int j=0;j<arr.length;j++)
                   {
                       if(!arr[j].startsWith("0"))
                               {
                                   int n=Integer.parseInt(arr[j]);
                                   int d=0;
                                   while(n>0)
                           {
                           d=n%10; //extracting digit from the end
                           freq[d]++; //increasing the frequency of that digit.
                           n=n/10;
                           }
                               }
                   }
                  
                  
               }
                  
           }
           for(int k=1;k<10;k++)
           {
               System.out.print(k+" ");
               System.out.print(freq[k]+" ");
               float perfreq=(float)freq[k]/100;
               System.out.println(perfreq);
           }
       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (br != null)
                   br.close();

               if (fr != null)
                   fr.close();

           } catch (IOException ex) {

               ex.printStackTrace();

           }

       }

   }
}

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