I have an assignment where I have two classes, a driver class which uses the scanner utility to read string from the keyboard and then it records it letter frequencies. (How many times each letter appears in the inputted string). My program should continue entering lines of text until you type two Returns in a row. Then the 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 my code ignores letter case - so capital letters as well as lower case letters should be counted.
My driver class is
import java.util.*;
public class LetterDriver{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String tScan = " ";
while(tScan.length() > 0){
tScan = s.nextLine();
}
}
}
My actual profile class is
public class LetterProfile {
int score[] = 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)
score[next]++;
}
}
public void scoreLine (String lines) { // prints letter + # of appearance
int index = lines.length();
for (int j = 0; j<index; j++) {
score[j]++;
System.out.println(j + score[j]);
}
}
public int largestLength() { // finds most frequent letter
int largest = 0;
int largestindex = 0;
for(int a = 0; a<26; a++)
if(score[a]>largest){
largest = score[a];
largestindex = a;
}
return largestindex;
}
public void printResults() {
largestLength();
System.out.println(largestLength());
}
}
Again my code compiles and when i run it allows me to enter my text input but when I return twice all i get is blank output. I think it may have to do with my profile class not reading correctly from my driver class but can't figure out whats wrong.
You are just reading the data from the scanner in your main method you haven't instantiated your class LetterProfile in your Main method, so it does nothing.
You aren't using LetterProfile class anywhere!! You are just reading the input but not passing in to LetterProfile. Instantiate LetterProfile class in your driver and call the relevant methods.
Your driver class may look like:
public class LetterDriver{
public static void main(String[] args){
LetterProfile letterProfile = new LetterProfile();
Scanner s = new Scanner(System.in);
String tScan = " ";
while(tScan.length() > 0){
tScan = s.nextLine();
letterProfile.countChars(tScan);
}
// Print the result
letterProfile.printResults()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With