>
Click HERE for the ASCII Table for different Characters
The Character Class: (Part 1)
Methods of Character class:
-
Example: Method -> isDigit() [validating name]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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!!"); } } } -
Example: Method -> isLetter() [validating mobile number]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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!! */ -
Example: Method -> isLetterOrDigit() [validating user_id / username]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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!! */ -
Example: Method -> isLowerCase()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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: ! */ -
Example: Method -> isUpperCase()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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 */ -
Example: Method -> toUpperCase() and toLowerCase()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// 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 */
Click HERE for the ASCII Table for different Characters
Comments
Post a Comment