11.4 can this be done in java please Complete this application by coding the add
ID: 3777930 • Letter: 1
Question
11.4 can this be done in java please
Complete this application by coding the additional methods. The main() method follows:
import java.io.*;
import java.util.*;
public class LetterCounter
{
public static void main(String[] args)
{
String sLetters[] = new String[52];
int iCounts[] = new int[52];
int iSub;
fvPopulateCounterArray(sLetters);
fvReadAndCount("c:/home/LetterCounterData.txt", sLetters, iCounts);
fvShowLetterCount(sLetters, iCounts);
}
Tips and starter code:
The data file is in Blackboard. Save it to the location in the method call.
In fvPopulateCounterArray, this will be an important piece of the code. You'll also need a similar block to handle uppercase letters.
for (iLtr = 'a'; iLtr <= 'z'; iLtr ++)
{
sLtr = String.valueOf((char) iLtr);
psLetters[iSub] = sLtr;
iSub ++;
}
Here's the method header to use for fvReadAndCount:
public static void fvReadAndCount(String psFile, String psLetters[], int piCounts[])
In fvReadAndCount, you'll need to extract each line, then in each line, extract each character. Then, you have to test the character against each of the 52 letters. If you get a match, then you need to increment the letter count:
for (iSub = 0; iSub <= 51; iSub ++)
{
if (psLetters[iSub].equals(sChar))
{
piCounts[iSub] = piCounts[iSub] + 1;
}
}
If it works, you should get this output:
a: 4
b: 0
c: 1
d: 2
e: 9
f: 2
g: 1
h: 3
i: 3
j: 0
k: 0
l: 4
m: 2
n: 0
o: 9
p: 3
q: 0
r: 2
s: 2
t: 7
u: 0
v: 0
w: 1
x: 0
y: 1
z: 0
A: 0
B: 0
C: 0
D: 0
E: 0
F: 0
G: 0
H: 0
I: 0
J: 0
K: 0
L: 0
M: 0
N: 1
O: 0
P: 2
Q: 0
R: 1
S: 1
T: 0
U: 0
V: 1
W: 0
X: 0
Y: 0
Z: 0
Explanation / Answer
Here is the code for you:
import java.io.*;
import java.util.*;
public class LetterCounter
{
public static void main(String[] args) throws FileNotFoundException
{
String sLetters[] = new String[52];
int iCounts[] = new int[52];
int iSub;
fvPopulateCounterArray(sLetters);
fvReadAndCount("LetterCounterData.txt", sLetters, iCounts);
fvShowLetterCount(sLetters, iCounts);
}
public static void fvPopulateCounterArray(String[] sLtr)
{
int iSub = 0;
for (int iLtr = 'a'; iLtr <= 'z'; iLtr ++)
{
sLtr[iSub] = String.valueOf((char) iLtr);
iSub ++;
}
for (int iLtr = 'A'; iLtr <= 'Z'; iLtr ++)
{
sLtr[iSub] = String.valueOf((char) iLtr);
iSub ++;
}
}
public static void fvShowLetterCount(String[] sLetter, int[] iCounts)
{
for(int i = 0; i < 52; i++)
System.out.println(sLetter[i] + ": " + iCounts[i]);
}
public static void fvReadAndCount(String psFile, String psLetters[], int piCounts[]) throws FileNotFoundException
{
//In fvReadAndCount, you'll need to extract each line, then in each line, extract each character.
//Then, you have to test the character against each of the 52 letters. If you get a match,
//then you need to increment the letter count:
for(int i = 0; i < 52; i++)
piCounts[i] = 0;
Scanner file = new Scanner(new File(psFile));
while(file.hasNextLine())
{
String temp = file.nextLine();
for(int i = 0; i < temp.length(); i++)
{
char sChar = temp.charAt(i);
for (int iSub = 0; iSub <= 51; iSub ++)
if (psLetters[iSub].equals(sChar))
piCounts[iSub] = piCounts[iSub] + 1;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.