Advertisement
prashandip

CharacterCheck

Nov 26th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. public class CahracterCheck {
  2.  
  3.     public static void main(String[] args) {
  4.         Scanner scanner = new Scanner(System.in); // Create a Scanner object
  5.         String input = ""; // Initialize input string
  6.         // do-while loop
  7.         do {
  8.             System.out.print("Enter a character: "); // ask for a character
  9.             input = scanner.next(); // Read user input
  10.             String charType = getCharacterType(input); // get the character type
  11.             System.out.println("Character is: " + charType); // Output user input
  12.             System.out.println("---------------------"); // new line
  13.         } while (!isEnd(input)); // loop runs until "done" is encountered
  14.         // encountered
  15.         scanner.close();
  16.         System.out.println("===== THANK YOU =====");
  17.     }
  18.  
  19.     // function to check the character type
  20.     private static String getCharacterType(String input) {
  21.         if (input.length() > 1) {
  22.             if (isEnd(input)) {
  23.                 return "TERMINATION"; // if the input is "done" return the type "TERMINATION"
  24.             } else {
  25.                 return "INVALID"; // return invalid as the input contains more than one charcacter and is not
  26.                                     // "done"
  27.             }
  28.         } else {
  29.             char c = input.charAt(0); // getting the character at 0 position of input
  30.             // ASCII Values
  31.             // 0-9 >> 48-57
  32.             // A-Z >> 65-90
  33.             // a-z >> 97-122
  34.             if (c >= 48 && c <= 57) {
  35.                 return "NUMBER"; // from 48 till 57 the character is number
  36.             } else if (c >= 65 && c <= 90) {
  37.                 return "CAPITAL LETTER";// from 65 till 90 the character is capital letter
  38.             } else if (c >= 97 && c <= 122) {
  39.                 return "SMALL LETTER";// from 97 till 122 the character is small letter
  40.             } else {
  41.                 return "SPECIAL SYMBOL";// else the character is special symbol
  42.  
  43.             }
  44.         }
  45.     }
  46.  
  47.     // check if the program is to end
  48.     private static boolean isEnd(String input) {
  49.         if (input.length() == 4) {
  50.             char[] c = input.toCharArray(); // input string converted to char array for simplicity
  51.             if ((c[0] == 'D' || c[0] == 'd') && (c[1] == 'O' || c[1] == 'o') && (c[2] == 'N' || c[2] == 'n')
  52.                     && (c[3] == 'E' || c[3] == 'e')) {
  53.                 return true; // if DONE is encountered in any combination eg; DoNe, done, DONE, donE, etc the
  54.             } // program ends
  55.             else {
  56.                 return false; // else the program doesn't end as the input is not "DONE"
  57.             }
  58.         } else {
  59.             return false; // if input length is not 4 the program continues as "done" contains exactly 4
  60.         } // letters
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement