+ Reply to Thread
Results 1 to 5 of 5

Thread: Java: Character Class

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Java: Character Class

    Java: The Character Class

    Every primitive data type has a wrapper class that provides methods for manipulating the data type. There is a lot of really useful functions in the Character class that we are going to look at.

    These methods are all static methods, and thus you do not have to create an instance of the character class. Also, you do not have to import anything.

    We are going to use this string below to play around with the Character class and the available methods:

    Code:
    String s = "Hello.There!!!3233 67yt$";
    We are going to do these things:

    1. Count the number of upper case letters
    2. Count the number of lower case letters
    3. Count the number of spaces
    4. Count the number of digits

    The Character class provides methods to do all of these things. To check if letters are in uppercase, we use the method isUpperCase? To count the lowercase letters, we use the method isLowerCase. To count the number of spaces, we use the method isSpaceChar.

    As a matter of efficiency, you can do all these operations in one iteration of the String. However, I am going to use several iterations to emphasis the different methods.

    All of these methods return true or false indicating what the current character is.

    A method we are going to use a lot is the s.charAt method. This returns an individual char from the string.You can think of this as a direct-access to an item in an array.

    Counting Upper Case Letters

    The method we are going to use is the method Character.isUpperCase. We need a counter for the upper case letters.

    Code:
    int nUpper = 0;
    This variable is going to count how many upper case letters there are.

    Code:
    for (int i=0;i<s.length();i++) {
    	if (Character.isUpperCase(s.charAt(i))) {
    		nUpper++;
    	}
    }
    System.out.println("s contains " + nUpper + " upper case letters.");
    The output is:

    s contains 2 upper case letters.
    All we have to do is iterate through the String, checking if each letter is upper case and if it is then we update the counter by 1.

    Count Lower Case Letters

    We need another counter to count the number of lower case letters.

    Code:
    int nLower = 0;
    Again, a simple iteration and if structure works well here.

    Code:
    for (int i = 0; i < s.length(); i++) {
                if (Character.isLowerCase(s.charAt(i))) {
                    nLower++;
                }
            }
    System.out.println("s contains " + nLower + " lower case letters.");
    Output:

    s contains 10 lower case letters.
    Counting Spaces

    Note, that there is a method isSpace but it is a deprecated method and you shold use isSpaceChar instead. This again is just a simple iteration and counting task.

    Code:
    int nSpaces = 0;
    
    for (int i=0;i<s.length();i++) {
    	if (Character.isSpaceChar(s.charAt(i))) {
    		nSpaces++;
    	}
    }
    System.out.println("s contains " + nSpaces + " spaces.");
    Output:

    s contains 1 spaces.
    Counting Digits

    Code:
    int nDigit = 0;
    
    for (int i=0;i<s.length();i++) {
    	if (Character.isDigit(s.charAt(i))) {
    		nDigit++;
    	}
    }
    System.out.println("s contains " + nDigit + " numbers.");
    Output:

    s contains 6 numbers.
    This is just the same as above. I don't need to comment on it any. If Character.isDigit returns true then we find a digit and we increment the counter.

    These methods provide a great and fast way to manipulate characters. There are lots of available methods. You just have to take a look and see what is there. This is a vary useful class.
    Last edited by chili5; 09-01-2009 at 10:15 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Java: Character Class

    +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Java: Character Class

    Nice! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Java: Character Class

    Awesome! +rep
    Wow I changed my sig!

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Java: Character Class

    Great post +rep, but that smilie is a little weird

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 10-12-2011, 06:24 AM
  2. Replies: 13
    Last Post: 05-20-2011, 01:45 PM
  3. Replies: 1
    Last Post: 02-17-2011, 11:21 AM
  4. separate class help in java
    By jun71178 in forum Java Help
    Replies: 2
    Last Post: 02-16-2011, 09:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

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