3

parseInt メソッドを使用して String の最初の 2 文字を変換しようとしていますが、変換できません。次のようになります。

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.

たとえば、最初の 2 文字が 98 の場合、その部分文字列を int に変換します。

4

1 に答える 1

1

まあ、文字列が呼び出されfirstCharsて parse しようとしているという事実を除けfirstCharば、それはうまくいくはずです。

ただし、これはブレークポイントを備えたデバッガーを使用して、変数に配置されている値を把握するか、単に出力する必要がある領域です。

  • IntMessage部分文字列を実行する前に (オブジェクトの場合、これは通常小文字で始まるべきではありませんか?)。
  • firstChars部分文字列を実行した後(たとえば、数値であることを確認してください)。
  • Integer.parseInt(firstChars)その後、期待どおりであることを確認します。
  • それからInteger.parseInt(firstChars) - 32
  • 最後に、ASCII[Integer.parseInt(firstChars) - 32].

次に、すべての出力を調べて、問題が何であるかを確認するのは簡単なことです。

于 2011-09-29T01:55:53.507 に答える