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(with step by step instructions). Create public

ID: 3794760 • Letter: U

Question

Use Java to program the following(with step by step instructions).

Create public java class named Benford.

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.

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

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

Your output must be formatted as follows.

This is what I have so far:

I'm having issues with files(like sunspots.txt) that has words in them.

file:

sunspots Notepad File Edit Format View Help ( Sunspot data collected by Robin McQuinn from *) (* http://sidc.oma.be/html/sunspot.html (* Month: 1749 01 *) 58 (* Month: 1749 02 ) 63 (* Month: 1749 03 *) 70 (* Month: 1749 04 *) 56 (* Month: 1749 05 *) 85 (* Month: 1749 06 *) 84 (* Month: 1749 07 *) 95 (* Month: 1749 08 ) 66 (* Month: 1749 09 *) 76 (* Month: 1749 10 *) 76 (* Month: 1749 11 *) 159 (* Month: 1749 12 *) 85 (* Month: 1750 01 *) 73 (* Month: 1750 02 *) 76 (* Month: 1750 03 *) 89 (* Month: 1750 04 *) 88 (* Month: 1750 05 *) 90 (* Month: 1750 06 *) 108 (* Month: 1750 07 *) 85 (* Month: 1750 08 *) 103 (* Month: 1750 09 *) 91

Explanation / Answer

import java.io.File;

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

public class Benford {
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(System.in);

System.out.print("Please enter name of file: ");
String fileName = s.next();

Scanner inputFile = new Scanner(new File(fileName));
int[] frequency = new int[10];
int totalCount = 0;

while(inputFile.hasNextLine()){
String line = inputFile.nextLine();
if(line == null || line.isEmpty() || line.trim() == "")
continue;

for(int i=0;i<line.length();i++){
   char firstDigit = line.charAt(i);

   // I'm assuming comments start with '*'
   if(firstDigit == '*'){
    break;
}
   else if(Character.isDigit(firstDigit) && firstDigit != '0'){

int d = firstDigit - '0';
frequency[d]++;
totalCount++;
break;
}
}
}
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));
}
}
}

input file:

/ welcome /

apple 1
3
*df4
4
sdkfj2kfd
0r9dskfj
3fld

Output:

Digit Count Frequency
1     1    16.666666666666664
2     1    16.666666666666664
3     2    33.33333333333333
4     1    16.666666666666664
5     0    0.0
6     0    0.0
7     0    0.0
8     0    0.0
9     1    16.666666666666664

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