解析一个子字符串? [英] Parse a substring?

查看:126
本文介绍了解析一个子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用parseInt方法转换字符串的前两个字符,但我不能。它应该看起来像这样:

  String firstChars = IntMessage.substring(0,2); // firstChars是一个String对应于字符串的前两个字符。 

message = ASCII [(Integer.parseInt(firstChar)) - 32]; //消息变量是一个字符串,它应该接受一个firstChars变量并使其成为一个整数,以便可以使用由ASCII数组确定要将数组的哪个元素连接到消息String。

例如,如果前两个字符都是98,我想取这个子字符串并将其转换为int。

解决方案

嗯,除了你的字符串被调用的事实 firstChars 并且你正在尝试解析 firstChar ,这应该可以正常工作。



你应该使用一个带有断点的调试器,这样你可以知道变量中的值是什么,或者打印出来:




  • IntMessage 在执行子字符串之前(如果它是一个对象,通常不以小写字母开头)。

  • firstChars 之后(例如,确保它是数字的)。

  • Integer.parseInt )之后,确保它是你期望的。

  • 然后 Integer.parseInt(firstChars) - 32

  • 最后, ASCII [Integer.parseInt(firstChars) - 32]



    • 然后,这将是一个简单的事情,检查所有的输出,以查看问题是什么。


      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.

      解决方案

      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.

      这篇关于解析一个子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆