Skip to main content

Character Class in Java (Part 2)

>

The Character Class: (Part 1)

Methods of Character class:


  • Example: Method -> isDigit() [validating name]
    // Using isDigit() to check whether name or any string entered by user has a digit or not.
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner sc=new Scanner(System.in);
    boolean flag =false; // keep the switch/flag off in the beginning.
    System.out.println("Enter name:");
    String name = sc.nextLine();
    // extract each character from name by charAt() method.
    for(int i=0;i<name.length();i++)
    {
    char ch = name.charAt(i); // get the 0th character, 1st character and so on..
    // if ch is a digit, its invalid for name.
    if(Character.isDigit(ch))
    {
    flag=true; // if the digit is found, set flag to true.
    System.out.println("NAME CANNOT CONTAIN DIGITS.. ");// no need to check rest of characters now. so break the loop.
    break;
    }
    }
    if(flag==false) // if flag was still holding old value (false), then the string/name is valid.
    {
    System.out.println("VALID NAME!!");
    }
    }
    }
    view raw test.java hosted with ❤ by GitHub
  • Example: Method -> isLetter() [validating mobile number]
    // Using isLetter() to check whether contact number or any integer input entered by user has a letter or not.
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner sc=new Scanner(System.in);
    boolean flag =false; // keep the switch/flag off in the beginning.
    System.out.println("Enter phone number:");
    String number = sc.nextLine();
    if(number.length()==10){
    // extract each character from number by charAt() method.
    for(int i=0;i<number.length();i++)
    {
    char ch = number.charAt(i); // get the 0th character, 1st character and so on..
    // if ch is a letter, its invalid for contact number.
    if(Character.isLetter(ch))
    {
    flag=true; // if a letter is found, set flag to true.
    System.out.println("MOBILE NUMBER CANNOT CONTAIN LETTERS.. ");// no need to check rest of characters now. so break the loop.
    break;
    }
    }
    if(flag==false) {// if flag was still holding old value (false), then the contact-number is valid.
    System.out.println("VALID MOBILE NUMBER!!");
    }
    }
    else
    {
    System.out.println("INVALID LENGTH MOBILE NUMBER!!");
    }
    }
    }
    /* OUTPUT:
    Enter phone number:
    123
    INVALID LENGTH MOBILE NUMBER!!
    Enter phone number:
    121313DS
    INVALID LENGTH MOBILE NUMBER!!
    Enter phone number:
    123456789P
    MOBILE NUMBER CANNOT CONTAIN LETTERS..
    Enter phone number:
    1234567890
    VALID MOBILE NUMBER!!
    */
    view raw test.java hosted with ❤ by GitHub
  • Example: Method -> isLetterOrDigit() [validating user_id / username]
    // Using isLetterOrDigit() to check whether user_id or any string entered by user has a digit and/or letter and no other symbols or characters.
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner sc=new Scanner(System.in);
    boolean flag =false; // keep the switch/flag off in the beginning.
    System.out.println("Enter user_id:");
    String user_id = sc.nextLine();
    // extract each character from user_id by charAt() method.
    for(int i=0;i<user_id.length();i++)
    {
    char ch = user_id.charAt(i); // get the 0th character, 1st character and so on..
    // if ch is a letter and/or digit, its valid for user_id. If not, its invalid.
    if(! Character.isLetterOrDigit(ch)) // using ! to check the invlaid condition.
    {
    flag=true; // if any special symbol is found, set flag to true.
    System.out.println("USER ID CAN ONLY CONTAIN LETTERS AND DIGITS.. ");// no need to check rest of characters now. so break the loop.
    break;
    }
    }
    if(flag==false) {// if flag was still holding old value (false), then the user_id is valid.
    System.out.println("VALID USER ID!!");
    }
    }
    }
    /* OUTPUT
    Enter user_id:
    user_1
    USER ID CAN ONLY CONTAIN LETTERS AND DIGITS..
    Enter user_id:
    user@123
    USER ID CAN ONLY CONTAIN LETTERS AND DIGITS..
    Enter user_id:
    user1
    VALID USER ID!!
    */
    view raw test.java hosted with ❤ by GitHub
  • Example: Method -> isLowerCase()
    // Using isLowerCase() to check whether name or any string entered by user is lowercase.
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter any string:");
    String name = sc.nextLine();
    // extract each character from user_id by charAt() method.
    for(int i=0;i<name.length();i++)
    {
    char ch = name.charAt(i); // get the 0th character, 1st character and so on..
    // if ch is a lowercase character or not
    if(Character.isLowerCase(ch))
    {
    System.out.println("Lowercase character: "+ ch);
    }
    else
    {
    System.out.println("Non-Lowercase character: "+ ch);
    }
    }
    }
    }
    /* OUTPUT:
    Enter any string:
    hEllo!
    Lowercase character: h
    Non-Lowercase character: E
    Lowercase character: l
    Lowercase character: l
    Lowercase character: o
    Non-Lowercase character: !
    */
    view raw test.java hosted with ❤ by GitHub
  • Example: Method -> isUpperCase()
    // Using isUpperCase() to check whether name or any string entered by user is uppercase or not.
    import java.util.Scanner;
    class test
    {
    public static void main(String ar[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter any string:");
    String name = sc.nextLine();
    // extract each character from name by charAt() method.
    for(int i=0;i<name.length();i++)
    {
    char ch = name.charAt(i); // get the 0th character, 1st character and so on..
    // if ch is a uppercase character or not
    if(Character.isUpperCase(ch))
    {
    System.out.println("Uppercase character: "+ ch);
    }
    else
    {
    System.out.println("Non-Uppercase character: "+ ch);
    }
    }
    }
    }
    /* OUTPUT:
    Enter any string:
    JaVA
    Uppercase character: J
    Non-Uppercase character: a
    Uppercase character: V
    Uppercase character: A
    */
    view raw test.java hosted with ❤ by GitHub
  • Example: Method -> toUpperCase() and toLowerCase()
    // Convert uppercase to lowercase and vice-versa.
    import java.util.Scanner;
    class test {
    public static void main(String ar[]) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter any string:");
    String name = sc.nextLine();
    String ans="";
    // extract each character from user_id by charAt() method.
    for(int i=0;i<name.length();i++)
    {
    char ch = name.charAt(i); // get the 0th character, 1st character and so on..
    if(Character.isUpperCase(ch)) // if character is uppercase, convert to lowercase and append in the final ans.
    {
    ans = ans + Character.toLowerCase(ch);
    }
    else
    {
    ans = ans + Character.toUpperCase(ch); // vice-versa
    }
    }
    System.out.println("Converted String: "+ans);
    }
    }
    /* OUTPUT:
    Enter any string:
    Java Programming
    Converted String: jAVA pROGRAMMING
    */
    view raw test.java hosted with ❤ by GitHub
Click HERE for the list of Methods of Character class.
Click HERE for the ASCII Table for different Characters

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...