Use Java to program the following. 1. Create public java class named Benford. 2.
ID: 3782689 • 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
void findCounts(int arr[], int n)
{
int i = 0;
while (i < n)
{
if (arr[i] <= 0)
{
i++;
continue;
}
int eleInd = arr[i] - 1;
if (arr[eleInd] > 0) {
arr[i] = arr[eleInd];
arr[eleInd] = -1;
}else {
arr[eleInd]--;
arr[i] = 0;
i++;
}
}
System.out.println("Below are counts of all elements");
System.out.println("Digit Count");
for (int j = 0; j < n; j++)
System.out.println(j+1 + " " + Math.abs(arr[j]));
}
public class Main {
private static final String FILENAME = "data.txt";
public static void main(String[] args) {
int arr[]=new int[10];
int i=0;
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
arr[i++] = sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Main count = new Main();
count.findCounts(arr, arr.length);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.