解析子串?

发布于 2024-12-06 19:36:36 字数 534 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2024-12-13 19:36:36

好吧,除了您的字符串名为 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 parse firstChar, 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.
  • Then Integer.parseInt(firstChars) - 32.
  • Finally, ASCII[Integer.parseInt(firstChars) - 32].

Then it will be a simple matter of examining all the outputs to see what the problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文