+ 2

Given code shows me an error in line no.13 , need help.

import java.lang.*; import java.io.*; public class ASCII { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in())); System.out.println("Enter any character or number"); String ch = reader.readLine(); System.out.println("You have entered :-" + ch); System.out println("ASCII Value :-"+(char) ch);//Line no.:- 13 } catch (Exception e) { System.out.println("Error"); } } } //program gives ascii value

13th Sep 2017, 11:40 AM
Hemant Jaiswal
Hemant Jaiswal - avatar
2 Antworten
+ 6
In Java, you can't cast String to character. You may use charAt(int index) here. In the case of your code, replace (char)ch with ch.charAt(0).
13th Sep 2017, 3:38 PM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
+ 2
thanks orb
13th Sep 2017, 6:06 PM
Hemant Jaiswal
Hemant Jaiswal - avatar