3

我正在尝试使用 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。

4

1 回答 1

1

好吧,除了您的字符串被调用firstChars并且您正在尝试解析之外firstChar,这应该可以正常工作。

但这是一个你应该使用带断点的调试器的区域,这样你就可以找出变量中放置了哪些值,或者只是将它们打印出来:

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

然后,检查所有输出以查看问题所在将是一件简单的事情。

于 2011-09-29T01:55:53.507 回答