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 char开发者_C百科acters are 98, I want to take that substring and convert it into an int.
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.
精彩评论