- 1

Why we can't convert string consists of alphabet to integer ?

9th Jan 2017, 10:43 AM
Gagan Kalra
Gagan Kalra - avatar
5 Antworten
0
I don't know if I get your question correct but there is a way we can convert all the alphabets in the string to their ASCII value. e.g. String str = "ABC"; int a = str.charAt(0); // a will print 65
9th Jan 2017, 10:55 AM
Turtle
0
but why we can't change it directly through string method only
9th Jan 2017, 11:17 AM
Gagan Kalra
Gagan Kalra - avatar
0
The simple reason for this is that letters can not be converted into numbers. There is only a way to get the letter's ASCII code by treating them as chars, Like @Turtle did it in his answer.
9th Jan 2017, 11:17 AM
Andreas K
Andreas K - avatar
0
what is benefits of this convert??
9th Jan 2017, 12:12 PM
Ali Akbar Moeini
Ali Akbar Moeini - avatar
0
Because String is an object and not a char so you cannot type cast it and the Integer.parseInt method does not support letters.
9th Jan 2017, 12:28 PM
Andreas K
Andreas K - avatar