I have a programming assignment that I can't figure out. Please help!
For this assignment you are to print a table that gives approximate letter frequencies for English text. To keep things simple and concrete, your job will be to print out the letter frequencies for multiple lines of text you enter from the keyboard. More specifically, you program should continue entering lines of text until you type two Returns in a row. Then your code should print the letter frequencies, followed by a report that gives the most frequent letter and its count (in case of ties for most frequent letter, any most frequent letter will do). Also, your code should ignore letter case - so capital letters as well as lower case letters should be counted.
Enter lines of text, type two returns in a row to end
now is the time
The quick brown fox jumped over the LAZY dog
and why not?
a 2
b 1
c 1
d 3
e 6
f 1
g 1
h 4
i 3
j 1
k 1
l 1
m 2
n 4
o 6
p 1
q 1
r 2
s 1
t 5
u 2
v 1
w 3
x 1
y 2
z 1
most frequent: e 6
Tips/Further Qualifications
Use the input style that's used in Program Backwards in Chapter 7. (It accepts multiple lines of text). However, there's no need to store the entered lines in an array (as happens in Program Backwards). For this assignment you can process each line after it's been entered in one pass, and then, in effect, you can throw it away.
You MUST do this assignment with two classes: a driver, which handles the input, and is called LetterDriver, and a principal processing class, called LetterProfile. Objects in the LetterProfile class are responsible for handling the character processing. LetterProfile must have an array instance variable that functions like a scoreboard, tallying letter frequencies as lines are read.
Convert all text as it comes in to lower case. Ignore characters that aren't letters. Remember that if someLetter is a char variable that holds a letter, then (someLetter-'a') gives the numerical position of someLetter among all lower case letters. Thus if someLetter holds an 'e', then (someLetter-'a') = 4. Use this fact to guide the updating of your letter frequency array scoreboard.
As always, be sure to comment your code.
Backwards Program:
import java.util.*;
public class Backwards{
public static void main(String[] args){
String[] lines = new String[50];
Scanner scan = new Scanner(System.in);
int pos = 0;
String t = " ";
while(t.length() > 0){
t = scan.nextLine();
lines[pos] = t;
pos++;
}
for(int j = pos - 1; j >= 0; j--){
lines[j] = lines[j].toUpperCase();
System.out.println(lines[j]);
}
}
}
Here is what I have so far:
Driver Class:
import java.util.*;
public class LetterDriver{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String tScan= " ";
while(tScan.length() > 0){
tScan = scan.nextLine();
}
}
}
Profile class:
public class LetterProfile{
int scoreboard[] = new int [26];
public void countChars (String s)
{
s.toLowerCase();
char a = 'a';
for (int i =0;i < s.length();i++){
int next = (int)s.charAt(i)-(int) a;
if ( next<26 && next >= 0) //if the character is a-z
scoreboard[next]++;
}
}
public void scoreLine (String line){
int index = line.length();
for (int j = 0; j<index; j++)
scoreboard[j]++;
System.out.println(j + scoreboard[j]);
}
public int largestLength(){
int largest = 0;
int largestindex = 0;
for(int a = 0; a<26; a++)
if(scores[a]>largest)
largest = scores[a];
largestindex = a;
return(char)largestindex;
}
public void printResults(){
System.out.println(largestLength());
}
}
I am getting so many errors and I am horrible at java programming and can't fix it );
Any advice would be appreciated! Thanks!