Parse a substring?

本秂侑毒 提交于 2019-12-10 21:35:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7591603/parse-a-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!