In: Computer Science
Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below.
" Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code
A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code. To help a bit: Using their ASCII codes, a Caesar cipher was used to encrypt uppercase letters, uppercase letters, and digits. This means the shift for uppercase letters forces the encrypted data to still be in the A-Z range. Lowercase letters will still be in the a-z range, and digits will be in the 0-9 range.
Your job is it so create a frequency analysis of the alphabetic characters in the file. To do this: Read a record from the file, look at each character in the record, count the frequency for each letter A-Z and a-z. You can use an array to keep the counts. Display the frequency distribution (percentage for each letter) in a GUI. "
So to put this frankly, I'm a bit lost. I know how to read the file, but I'm having a mini-stroke figuring out how to count all of the characters and their frequency. This is what I've got to read the file.
BufferedReader inFile = null;
PrintWriter outFile = null;
try
{
inFile = new BufferedReader(new FileReader("filename.csv"));
outFile = new PrintWriter(new FileWriter("filelines.txt"));
String readData;
System.out.println ("---OUTPUT FILE BEING PRODUCED---\n");
int[] freqs = new int[26];
while ((readData = inFile.readLine()) != null)
{
outFile.println(readData);
}
}
finally
{
System.out.println ("\n\n---PROCESSING COMPLETED---\n");
if (inFile != null)
{
inFile.close();
}
if (outFile != null)
{
outFile.close();
}
}
Can anyone offer any insight/tips in how to go about this? Thanks for reading!
Here is the solution if you have any doubt then please write in the comment section.
Please give feedback.
Solution: I have written comments for each line for better understanding.
I have provided two solution:
1st which is used in program is : storing frequency of uppercase and lowercase seperately using array of size 123
2nd which is commented after program is : storing frequency of uppercase and lowercase combinely in the array of length 27
import java.io.*;
public class Main
{
//main method throws IOException because IOException is checked Exception
public static void main(String[] args) throws IOException
{
//BufferedReader and PrintWriter reference variables
BufferedReader inFile = null;
PrintWriter outFile = null;
try
{
//open files in read and write mode respectively
inFile = new BufferedReader(new FileReader("filename.csv"));
outFile = new PrintWriter(new FileWriter("filelines.txt"));
//String to store the line from file
String readData;
//print msg
System.out.println ("---OUTPUT FILE BEING PRODUCED---\n");
/*declare array of size 123 because ascii of a-b in the range 97-122
and ascii of A-B is in the range 65-90, and here i am storing
frequency seperately for lowercase character and uppercase character
so if you want to store frequency of both lowercase and uppercase
together i mean (Case insensitive) then you can use modulo to store
the frequency in the array of range 26 i will provide the same
implementation seperately so you can use anyone which you want*/
/*Here 65 index of array is to store frequncy of Capital A and 66
for Capital B and so on */
int[] freqs = new int[123];
/*count to store total number of character, i am counting only
digits and alphabetic character*/
int count=0;
//read from file until there is data in the file
while ((readData = inFile.readLine()) != null)
{
//loop to iterate through all character in a single line of file
for(int i=0;i<readData.length();i++)
{
/*if character is alphabetic [we are checking using ascii ]
then add frequency in the array*/
if((readData.charAt(i)>=65&&readData.charAt(i)<=90)||
(readData.charAt(i)>=97&&readData.charAt(i)<=122))
{
//increment count
count++;
//increment frequency of particular alphabate
freqs[readData.charAt(i)]+=1;
}
//if character is digit then increment only count
else if(readData.charAt(i)>=48&&readData.charAt(i)<=57)
{
count++;
}
}
}
//printing frequency of characters in file for your understanding
for(int i=65;i<=90;i++)
{
char ch=(char)i;
outFile.println(ch+" "+freqs[i]);
}
for(int i=97;i<=122;i++)
{
char ch=(char)i;
outFile.println(ch+" "+freqs[i]);
}
//printing total count
outFile.println("Total Count: "+count);
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
System.out.println ("\n\n---PROCESSING COMPLETED---\n");
if (inFile != null)
{
inFile.close();
}
if (outFile != null)
{
outFile.close();
}
}
System.out.println("Hello World");
}
}
/* This procedure is for if you want that frequency of a and A will be treated
as same
int[] freqs = new int[27];
/*count to store total number of character, i am counting only
digits and alphabetic character*/
/*
int count=0;
//read from file until there is data in the file
while ((readData = inFile.readLine()) != null)
{
//loop to iterate through all character in a single line of file
for(int i=0;i<readData.length();i++)
{
/*if character is alphabetic [we are checking using ascii ]
then add frequency in the array*/
/* if((readData.charAt(i)>=65&&readData.charAt(i)<=90))
{
//increment count
count++;
//here modulo 65 used so that A will have frequency at
//index 1 and B will have frequency at index 2 and so on
freqs[readData.charAt(i)%65+1]+=1;
}
else if(readData.charAt(i)>=97&&readData.charAt(i)<=122)
{
//increment count
count++;
//here modulo 97 used so that a will have frequency at
//index 1 and b will have frequency at index 2 and so on
freqs[readData.charAt(i)%97+1]+=1;
}
//if character is digit then increment only count
else if(readData.charAt(i)>=48&&readData.charAt(i)<=57)
{
count++;
}
}
}
*/
Input and Output:
filename.csv
abx,Abc,ABC
cde,RTY,QWEQWEQ,XCVXCV
tytyu,WERWER,fghfgh
output file: filelines.txt
A 2
B 1
C 3
D 0
E 4
F 0
G 0
H 0
I 0
J 0
K 0
L 0
M 0
N 0
O 0
P 0
Q 3
R 3
S 0
T 1
U 0
V 2
W 4
X 2
Y 1
Z 0
a 1
b 2
c 2
d 1
e 1
f 2
g 2
h 2
i 0
j 0
k 0
l 0
m 0
n 0
o 0
p 0
q 0
r 0
s 0
t 2
u 1
v 0
w 0
x 1
y 2
z 0
Total Count: 45
If you have any type of doubts then please write in the comment section, I will feel happy to help you.
Please give feedback.
Thank You!