Please write a java program for following condition First pr
Please write a java program for following condition:
First, prompt the user to enter the file name
For example, here’s the file named example.txt:
this is line one
ASDFASDFDDSS
this is line two
FDSAFDSAFFSS
this is line three
FDSa-FdsA-fdFD
here’s the corresponding numbers for each letter:
A or a : 123.4
S or s : 234.5
D or d : 345.6
F or f : 456.7
- : 10.0
output should show four things:
Line: this is line one
Letters: ASDFASDFDDSS
Letter Counts: [#, #, #, #]
(#=number of each letter. In order of A, S, D, F,)
Total %: [%, %, %, %] of total sum of the letters
(% = percentage of each letter in the total sum. also in order of A, S, D, F)
List: [ASD, FAS, DFD, DSS]
Line: this is line two
Letters: FDSAFDSAFFSS
Letter Counts: [#, #, #, #]
Total %: [%, %, %, %] of total sum of the letters
List: [FDS,AFD,SAF,FSS]
Line: this is line two
Letters: FDSA-FDSA-FDFD
Letter Counts: [#, #, #, #]
Total %: [%, %, %, %] of total sum of the letters
List: [FDS,AFD,SAF,DFD]
Solution
Program:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class WriteToFile {
private static final String FILENAME = \"example.txt\";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
double a = 123.4;
double s = 234.5;
double d = 345.6;
double f=456.7;
double colon= 10.0;
int x=0;
while ((sCurrentLine = br.readLine()) != null) {
int aC=0,sC=0,dC=0,fC=0,colonC=0;
if(x%2==0)
{
System.out.println(sCurrentLine);
x++;
}
else
{
System.out.println(\"LETTERS: \"+sCurrentLine);
for(int i=0;i<sCurrentLine.length();i++)
{
switch(sCurrentLine.toUpperCase().charAt(i))
{
case \'A\':
aC++;
break;
case \'S\':
sC++;
break;
case \'D\':
dC++;
break;
case \'F\':
fC++;
break;
case \'-\':
colonC++;
break;
}
}
x++;
System.out.println(\"Letter Counts: [\"+aC+\",\"+sC+\",\"+dC+\",\"+fC+\"]\");
double total=aC*a+sC*s+dC*d+fC*f+colonC*colon;
System.out.println(\"Total %: [\"+aC*a/total*100+\"%,\"+sC*s/total*100+\" %,\"+dC*d/total*100+\" %,\"+fC*f/total*100+\" %] of total sum of the letters\");
System.out.println(\"LIST:[\"+sCurrentLine.substring(0, 3)+\",\"+sCurrentLine.substring(3, 6)+\",\"+sCurrentLine.substring(6, 9)+\",\"+sCurrentLine.substring(9, 12)+\"]\");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Output:
this is line one
LETTERS: ASDFASDFDDSS
Letter Counts: [2,4,4,2]
Total %: [7.090731483077631%,26.949376544273978 %,39.7172901223927 %,26.242601850255703 %] of total sum of the letters
LIST:[ASD,FAS,DFD,DSS]
this is line two
LETTERS: FDSAFDSAFFSS
Letter Counts: [2,4,2,4]
Total %: [6.665226315220914%,25.33218105217673 %,18.66695473695582 %,49.335637895646535 %] of total sum of the letters
LIST:[FDS,AFD,SAF,FSS]
this is line three
LETTERS: FDSa-FdsA-fdFD
Letter Counts: [2,2,4,4]
Total %: [6.256020278833967%,11.888466413181241 %,35.041825095057035 %,46.30671736375158 %] of total sum of the letters
LIST:[FDS,a-F,dsA,-fd]


