Results 1 to 3 of 3
  1. #1
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default 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);
    }





    }
    }

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    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);
    	}
    }

  3. #3
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks, that's really helpful!

    I think I just need to get a bit better at being able to understand how to read the api! :)

Similar Threads

  1. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 08:28 PM
  2. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  3. Replies: 1
    Last Post: 01-15-2008, 09:55 PM
  4. Problem with display the character
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:43 PM
  5. Problem with '/' character in HTML and JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •