Reputation: 59
I wrote a Java web based program to display ASCII value of given character. but it is not working properly. pls help me. this is my code. no need to add characters to the database.
main bean
public mainbean() {}
private String charTest;
public String getCharTest() {
return charTest;
}
public void setCharTest(String charTest) {
this.charTest = charTest;
}
public String DoDisplay() throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
// System.out.println("Enter the char:");
// String str = buff.readLine();
String str = buff.readLine();
// for ( int i = 0; i < str.length(); ++i){
int i = 0;
char c = str.charAt(i);
int j = (int) c;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "ASCII OF" + c + "=" + j, ""));
return null;
}
this is my xhtml page
<h:head>
<title>Complain System</title>
</h:head>
<h:body>
<h:form id="form">
<h:outputText value="name"/>
<p:inputText value="#{mainbean.charTest}"/>
<p:commandButton value="submit" ajax="false" action="#{mainbean.doDisplay()}"/>
</h:form>
</h:body>
This is Ctrl page
public class charCtrl {
private EntityManager em;
public static void add() throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
String str = buff.readLine();
int i = 0;
char c = str.charAt(i);
int j = (int) c;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "ASCII OF" + c + "=" + j, ""));
}
}
Upvotes: 1
Views: 334
Reputation: 35491
You can typecast your char
as an int
. So:
int asciiVal = (int) yourChar;
Sample code:
char yourChar = 'a';
int asciiVal = (int) yourChar;
System.out.println("ASCII for " + yourChar + " is " + asciiVal);
Upvotes: 0
Reputation: 307
you can try 1.) String.valueOf(ch).codePointAt(0); 2.) cast the character with int
Upvotes: 1