Character Wrapper Class


Java / Wednesday, August 8th, 2018

Java Character class and methods

Java Character class and methods are placed in java.lang package. It is a wrapper class. Sometimes programmer needs to use objects instead of primitive data types. Character wrapper class is for primitive data type char. Package  java.lang is implicitly available to the user to write code.

Character Methods

These methods deal only with character manipulation in java. Some of them are explained below:

Character.isLetter()

This method is used to check whether a given argument is a letter or not. It returns a Boolean type value either true or false.
Syntax:
boolean var_name = Character.isLetter(char_name);
Or, boolean var_name = Character.isLetter(character);
Example:
boolean k = Character.isLetter(‘m’);
The method will return true to the variable k.
boolean k = Character.isLetter(‘8’);
It will return false.

Character.isDigit()

This method returns a boolean  type value true if a given argument is a digit, otherwise false.
Syntax:
boolean var_name = Character.isDigit(char_name);
Or, boolean var_name = Character.isDigit(character);
Example:
boolean k = Character.isDigit(‘8’);
It returns true to the variable k.
boolean k = Character.isDigit(‘M’);
It returns false to the variable k.

Character.isLetterOrDigit()

This method returns true if the given argument is either a letter or a digit, false otherwise.
Syntax:
boolean var_name = Character.isLetterOrDigit(char_name);
Or, boolean var_name = Character.isLetterOrDigit(character);
Example:
boolean k =  Character.isLetterOrDigit(‘G’);
It returns true to the boolean variable k as ‘G’ is a letter.
boolean k =  Character.isLetterOrDigit(‘5’);
It returns true to the boolean variable k as ‘5’ is a digit.
boolean k =  Character.isLetterOrDigit(‘*’);
It returns false to the boolean variable k as ‘*’ is neither a letter nor a digit.

Character.isWhitespace()

This method can be used to check for an existing blank or gap in a String. It will return a boolean value true if the given argument is a white space and false otherwise.
boolean var_name = Character.isWhitespace(char_name);
Or, boolean var_name = Character.isWhitespace(character);
Example:
boolean k =  Character.isWhitespace(‘ ’);
It returns true to the boolean variable k as the given character is blank.
boolean k =  Character.isLetterOrDigit(‘*’);
It returns false to the boolean variable k as ‘*’ is not a blank.

Character.isUpperCase()

This method will return true if the given argument is an upper case letter, otherwise false.
Syntax:
boolean var_name = Character.isUpperCase(char_name);
Or, boolean var_name = Character.isUpperCase (character);
Example:
boolean k =  Character.isUpperCase (‘ B’);
It returns true to the variable k.
boolean k =  Character.isUpperCase (‘ x’);
It returns false to the variable k.

Character.isLowerCase()

This method will return true if the given argument is a lower case letter, otherwise false.
Syntax:
boolean var_name = Character.isLowerCase(char_name);
Or, boolean var_name = Character.isLowerCase (character);
Example:
boolean k =  Character.isLowerCase (‘ b’);
It returns true to the variable k.
boolean k =  Character.isLowerCase (‘ X’);
It returns false to the variable k.

Character.toUpperCase()

This method returns the argument in upper case character. The given character remains same if it is already in upper case. It returns the same character if the character used with the method is not a letter.
Syntax:
char var_name = Character.toUpperCase (char_name);
Or, char var_name = Character.toUpperCase(character);
Example:
char x = Character.toUpperCase(‘a’);
It returns A to the variable x.
char x = Character.toUpperCase(‘P’);
It returns P to the variable x.
char x = Character.toUpperCase(‘?’);
It will return the same character ‘?’ because the given character is not a letter.

Character.toLowerCase()

This method returns the argument in lower case character. The given character remains same if it is already in lower case. It returns the same character if the character used with the method is not a letter.
Syntax:
char var_name = Character.toLowerCase (char_name);
Or, char var_name = Character.toLowerCase (character);
Example:
char x = Character.toLowerCase (‘a’);
It returns a to the variable x.
char x = Character.toLowerCase (‘P’);
It returns p to the variable x.
char x = Character.toLowerCase (‘?’);
It will return the same character ‘?’ because the given character is not a letter.

 Program 01

Write a program to accept a character and check the case (upper/ lower) otherwise, check whether it is a digit or a special character.

/**
 * @author Rajib
 */
import java.util.*;

public class CharType {
    public static void main(String k[])
    {
        Scanner sc = new Scanner(System.in);
        char ch;
        
        System.out.print("Enter a character: ");
        ch = sc.next().charAt(0);
        
        if(Character.isLetter(ch)==true)
        {
            System.out.println(ch + " is a letter");
            if(Character.isUpperCase(ch)==true)
                System.out.println(ch + " is an upper case letter");
            if(Character.isLowerCase(ch)==true)
                System.out.println(ch + " is a lower case letter");
        }
        else
        {
            if(Character.isDigit(ch)==true)
                System.out.println(ch + " is a digit");
            else
                System.out.println(ch + " is a special character");
        }
    }
    
}

 

 Program 02

Write a program to enter a letter. Display the new letter and ASCII code after changing the case.

Characters

ASCII Code
0-9

48-57

A-Z

65-90
a-z

97-122

/**
 *
 * @author Rajib
 */
import java.util.Scanner;

public class CharToAscii {
    public static void main(String k[])
    {
        Scanner sc = new Scanner(System.in);
        char ch, chr;
        
        System.out.print("Enter a character: ");
        ch = sc.next().charAt(0);
        
        if(Character.isUpperCase(ch)==true)
        {
            chr = Character.toLowerCase(ch);
            System.out.println("The lower case of " +ch + " is " + chr);
            System.out.println("The ASCII value of " +chr + " is " + (int)chr);
        }
        else
        {
            chr = Character.toUpperCase(ch);
            System.out.println("The upper case of " +ch + " is " + chr);
            System.out.println("The ASCII value of " +chr + " is " + (int)chr);
        }
    }
}

 

 Program 03

Write a program to accept a string. Count and display the number of vowels present in the string.

/**
 *
 * @author Rajib
 */

import java.util.*;
public class VowelCount 
{  
    public static void main(String args[])
    {        
        Scanner sc=new Scanner(System.in);        
        int counter=0,i;
        char temp;
        
        System.out.print("Enter any string: ");        
        String str=sc.nextLine();
       
        for(i=0;i<str.length();i++)
        {
            temp=str.charAt(i);            
            if(temp=='A' || temp=='a' || temp=='E'|| temp=='e' || temp=='I' || 
                    temp=='i' || temp=='O' || temp=='o' || temp=='U' || temp=='u')
            {                
                counter=counter+1;                
            }
        }
        System.out.println("The number of vowel present in "+ str +" is "+counter);
    }  
}

 

<< Previous     Next>>

;

Leave a Reply

Your email address will not be published. Required fields are marked *