I'm working on an assignment that requires a file to be read, and the frequency of letters to be returned. I currently have my return statement as my display method so that the letters in the file are shown in a table, and a counter for those letters is the next column over. Here's my problem: I have display method nested inside a for-loop that is nested in a while-loop, and currently returning nothing after the file has been read. The while loop is constructed so that it runs only as long as the file is being read (the end of the file being -1). The for loop is supposed to check to see if the index values of my inputValue is identical to the index values of the alphabet (and if so, display). As I've already mentioned, it's currently not displaying anything; and despite researching (and asking classmates) I've not been able to figure out what's wrong. Any insight that can be given would be greatly appreciated.
final static int AlphabetSize = 26;
final static Scanner cin = new Scanner(System.in);
final static int MaxBarLength = 50;
public static void main(String[] args) {
String fileName;
// sign-on
out.print("CPS 150 Assignment 6 by Anthony Lapham \n\n");
// get the file name
out.print("Enter the file name: ");
fileName = cin.nextLine();
// process the file
try {
processFile(fileName);
} catch (FileNotFoundException e) {
out.println(fileName + " was not found, program terminated");
} catch (IOException e) {
out.println("Unforeseen IO exception, stack trace follows");
e.printStackTrace();
} // end try
// sign-off
out.print("\nAssignment 6 complete\n\n");
} // end main
static void processFile(final String fileName)
throws FileNotFoundException, IOException {
FileInputStream inFile = new FileInputStream(fileName);
int inputValue = 0; // character read as an integer
char ch; // variable to hold input value typecast to char
int[] counters = new int[AlphabetSize];
final char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
// declare other variables you need
int i;
// implement a standard sentinel control loop here
int SENTINEL = -1;
// read first input character as an integer
inputValue = inFile.read();
while (inputValue != SENTINEL) {
ch = (char) inputValue;
for(i = 0; i < counters.length; i++){
if(char2int(ch) == alphabet[i]){
display(alphabet, counters);
}
}
// add code to process this character
// read next input character
inputValue = inFile.read();
} // end loop
inFile.close();
// generate appropriate output
} // end function
static void display(final char[] alphabet, final int[] counters) {
// write code for this function
int counter = 0;
int charCounter;
int num;
out.print("The file has " + alphabet + " alphabetic, and " + counters + " other characters.\n\n");
out.println("Letter Count Bar");
out.println("------ ----- ---");
// while (counter < AlphabetSize) {
//// out.printf("%5s %-4s %-3s", alphabet, counters, printChars(counters, alphabet));
// counter++;
// }
} // end function
// char2int is complete
static int char2int(final char arg) {
if (!Character.isLetter(arg)) {
return -1;
} else {
return (int) Character.toUpperCase(arg) - (int) 'A';
}
} // end function
// function printChars writes n copies of the character c to the
// standard output device
static void printChars(final int n, final char c) {
// write the code
for (int i = 0; i < n; i++) {
if (i == char2int(c)) {
out.print("*");
}
}
} // end printChars
// this function returns the largest value stored in the array
static int maxCount(final int[] arr) {
// write the code
return 0;
} // end function
}