Results 1 to 3 of 3
- 02-24-2009, 03:28 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
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);
}
}
}
- 02-24-2009, 03:43 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
This is the way to convert a character to upper case
Java Code:class A{ public static void main(String arg[]){ char ch='a'; System.out.println("Character is "+ch); ch=Character.toUpperCase(ch); System.out.println("Character is "+ch); } }
- 02-24-2009, 04:32 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
comparing array using character
By Anseki in forum New To JavaReplies: 7Last Post: 10-03-2008, 07:28 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Problem either with BufferedReader or sending a newline character
By aikanaro in forum NetworkingReplies: 1Last Post: 01-15-2008, 08:55 PM -
Problem with display the character
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 07:43 PM -
Problem with '/' character in HTML and JSP
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 07-04-2007, 05:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks