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
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.
Digit Count Frequency 1 3 0.30 2 2 0.20 3 2 0.20 4 0 0.00 5 0 0.00 6 0 0.00 7 2 0.20 8 0 0.00 9 1 0.10
This is what I have so far:
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; char firstDigit = line.charAt(0); if(Character.isDigit(firstDigit) && firstDigit != '0'){ int d = firstDigit - '0'; frequency[d]++; totalCount++; } } System.out.println("Digit"+ " "+"Count"+" "+"Frequency"); for(int i=1; iout.println(" "+i+" "+"\t"+frequency[i]+" " + "\t"+ ((double)frequency[i]/(double)totalCount*100)); } } }
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started