Find ASCII value of a character in Java
package test;
import java.util.Arrays;
public class charToInt {
public static void main(String[] args) {
char[] lowerChar = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] upperChar = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char[] specialChar = {'@','[','(','}','-'};
int[] lowerCharToNum = new int[26];
int[] upperCharToNum = new int[26];
int[] specialCharToNum = new int[specialChar.length];
for(int i=0;i<lowerChar.length;i++) {
char ch1 = lowerChar[i];
char ch2 = upperChar[i];
lowerCharToNum[i] = (int)ch1;
upperCharToNum[i] = (int)ch2;
//OR no need of Type Casting, it happens automatically, like below
lowerCharToNum[i] = ch1;
upperCharToNum[i] = ch2;
}
for(int i=0;i<specialChar.length;i++) {
char ch3 = specialChar[i];
specialCharToNum[i] = (int)ch3;
}
System.out.println("lowerCharToNum:\n"+Arrays.toString(lowerCharToNum));
System.out.println("upperCharToNum:\n"+Arrays.toString(upperCharToNum));
System.out.println("specialCharToNum:\n"+Arrays.toString(specialCharToNum));
}
}
Output:
lowerCharToNum:
[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
upperCharToNum:
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
specialCharToNum:
[64, 91, 40, 125, 45]
No comments:
Post a Comment