How can I convert a Char type to a Integer type?

凯文 2002-04-17 10:14:35
I want this function:
receive a char: '3' by System.in.read();
and convert it to integer 3;
but I always get 51 now.
I know 51 is the ASCII for 3;
How can I convert char '3' to integer 3?

waiting online...
...全文
57 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
凯文 2002-04-17
  • 打赏
  • 举报
回复
Yes you are right! Thank you!
xmvigour 2002-04-17
  • 打赏
  • 举报
回复
johnwesley:
i = System.in.read();得到的是ascii码,所以你必须转换成char,再转存为int。

xmvigour 2002-04-17
  • 打赏
  • 举报
回复
import java.util.*;
import java.io.*;

public class Test5{
public static void main(String[] args){
int i = 0;
char c;
int ii=0;
try{
i = System.in.read();
c=(char) i;
ii = Character.getNumericValue(c);
//ii =Integer.parseInt(String.valueOf(c));

System.out.println("Your input is:"+c+" ascii is "+i);
}
catch(IOException e){}

System.out.println("Length will be:"+ii);
}
}




stupidball 2002-04-17
  • 打赏
  • 举报
回复
add import java.lang.*;
凯文 2002-04-17
  • 打赏
  • 举报
回复
To: xmvigour(微电)

import java.util.*;
import java.io.*;

public class Test{
public static void main(String[] args){
int i = 0;
try{
i = System.in.read();
System.out.println("Your input is:"+i);
}
catch(IOException e){}

System.out.println("Length will be:"+i);
}
}
凯文 2002-04-17
  • 打赏
  • 举报
回复
to: tomxutomxu(shprog).

compile prompt: cannot resolve symbol
symbol: class OutputStreamReader
location: class Test
BufferedReader br = new BufferedReader(new OutputStreamReader(System.in)); ^
xmvigour 2002-04-17
  • 打赏
  • 举报
回复
johnwesley(我心荡漾(终于发现无界限)) :
can i view your code about this?
tomxutomxu 2002-04-17
  • 打赏
  • 举报
回复
System.in.read()读出来的是char,当然会是ascii值了。
BufferedReader br = new BufferedReder(new OutputStreamReader(System.in)));
String theNum = br.readLine();
int num = Integer.parseInt(theNum);
凯文 2002-04-17
  • 打赏
  • 举报
回复
But compile failed!
prompt:
getNumericValue(char) in java.lang.Character cannot be applied to (int)

Why?
xmvigour 2002-04-17
  • 打赏
  • 举报
回复
int ii = Character.getNumericValue(ss);

or

int ii =Integer.parseInt(String.valueOf(ss));

googles 2002-04-17
  • 打赏
  • 举报
回复
int ii = Character.getNumericValue(ss);
googles 2002-04-17
  • 打赏
  • 举报
回复
int ii = Character.getNumericValue(ss);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧