Java 解析子字符串?

Java 解析子字符串?,java,char,substring,parseint,Java,Char,Substring,Parseint,我正在尝试使用parseInt方法转换字符串的前两个字符,但无法转换。应该是这样的: String firstChars = IntMessage.substring(0,2);// firstChars is a String that corresponds to the first two characters of the string. message=ASCII[(Integer.parseInt(firstChar))-32];//The message variable is

我正在尝试使用parseInt方法转换字符串的前两个字符,但无法转换。应该是这样的:

String firstChars = IntMessage.substring(0,2);// firstChars is a String that corresponds to the first two characters  of the string.

message=ASCII[(Integer.parseInt(firstChar))-32];//The message variable is a String that is supposed to take a firstChars variable and make it an integer so it can be used by the ASCII array in determining which element of the array is to be concatenated to the message String.

例如,如果前两个字符是98,我想将该子字符串转换为int。

好吧,除了字符串名为
firstChars
并且您正在尝试解析
firstChar
,这应该可以正常工作

但在这一领域,您应该使用带断点的调试器,以便确定变量中放置了哪些值,或者直接打印出来:

  • IntMessage
    在执行子字符串之前(如果它是对象,通常不应该以小写字母开头?)
  • firstChars
    执行子字符串后(例如,确保它是数字)
  • Integer.parseInt(firstChars)
    之后,确保它是您所期望的
  • 然后
    Integer.parseInt(firstChars)-32
  • 最后,
    ASCII[Integer.parseInt(firstChars)-32]

然后,只需检查所有输出,看看问题出在哪里。

这看起来是/应该/可行的。您是否遇到特定的编译器/运行时错误?请定义“无法”。。。当然,发布实际的代码来演示这个问题。这是java程序,很糟糕