解析子串?
我正在尝试使用 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。
I'm trying to convert the first two characters of a String using the parseInt method but I cannot. It's supposed to look like this:
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.
For example if the first two characters are 98, I want to take that substring and convert it into an int.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,除了您的字符串名为
firstChars
并且您正在尝试解析firstChar
之外,这应该可以正常工作。但是在这个区域,您应该使用带有断点的调试器,以便您可以找出变量中放置的值,或者
IntMessage
(并且应该如果它是一个对象,它通常不是以小写字母开头吗?)。firstChars
(例如,确保它是数字)。Integer.parseInt(firstChars)
,确保它是您所期望的。Integer.parseInt(firstChars) - 32
。ASCII[Integer.parseInt(firstChars) - 32]
。然后检查所有输出以找出问题所在就很简单了。
Well, other than the fact that your string is called
firstChars
and you're trying to parsefirstChar
, that should work fine.But this is an area where you should either be using a debugger with breakpoints so you can figure out what values are being placed in the variables, or just print them out:
IntMessage
before doing the substring (and shouldn't this normally start with a lower case letter if it's an object?).firstChars
after doing the substring (make sure it's numeric, for example).Integer.parseInt(firstChars)
after that, making sure it's what you expect.Integer.parseInt(firstChars) - 32
.ASCII[Integer.parseInt(firstChars) - 32]
.Then it will be a simple matter of examining all the outputs to see what the problem is.