Problem using the toUpperCase method with character array...
Hello :-)
I am making a program which prints out letters in a text file and the frequency that they occur. (see code below)
However, I am having difficulty converting the characters to uppercase before they go into the array
The output should look like this:
Letter Frequency
A 23
B 13
etc..
I have tried inserting things such as this:
character = character.toUpperCase();
...and also lots of similar variations...but I keep getting 'int cannot be dereferenced'
I think I am using the .toUpperCase() method wrong but I'm not sure how!
Would anyone be able to explain what the error means and how I should use the method correctly in order to insert uppercase letters into my character array?
Thanks,
Lisa :-)
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Letters {
public static void main(String[] args) {
//check that an arg has been supplied - make this into a method called checkArgs
if (args.length == 0) {
System.out.println ("no filename specified");
System.exit(1);
}
//open stream to text file
try {
File textFile = new File(args[0]);
FileReader textFileReader = new FileReader(textFile);
BufferedReader textFileBuffer = new BufferedReader(textFileReader);
int character;
char[] characterArray = new char[100];
int z = 0;
while ((character = textFileBuffer.read()) != -1) { //while there is still stuff to read...
//System.out.println((char)character);
characterArray[z] = (char) character;
System.out.println(characterArray[z]);
z++;
}
textFileBuffer.close();
System.out.println("Letter Frequency");
// loop through all of the letters of the alphabet in uppercase
for (int characterIndex = 65; characterIndex <= 90; characterIndex++) {
int counter = 0;
//loop through each letter in the char array. compare each letter to the characterIndex. If they match then increment counter
for (int x = 0; x <= characterArray.length - 1; x++ ) {
if (characterIndex == (int) characterArray[x]) {
counter++;
}
}
System.out.println( " " + (char) + characterIndex + " " + counter);
}
}
catch(IOException ioe) {
System.out.println("Error opening file " + args[0]);
System.exit(2);
}
}
}