java - 解析子串?

标签 java char substring parseint

我正在尝试使用 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。

最佳答案

好吧,除了您的字符串名为 firstChars 并且您正在尝试解析 firstChar 之外,这应该可以正常工作。

但这是一个你应该使用带有断点的调试器的区域,这样你就可以弄清楚变量中放置了什么值,或者只是将它们打印出来:

  • IntMessage 在执行子字符串之前(如果它是一个对象,这通常不应该以小写字母开头吗?)。
  • firstChars 完成子字符串后(例如,确保它是数字)。
  • Integer.parseInt(firstChars) 之后,确保它符合您的预期。
  • 然后 Integer.parseInt(firstChars) - 32
  • 最后,ASCII[Integer.parseInt(firstChars) - 32]

然后检查所有输出以查看问题所在将是一件简单的事情。

关于java - 解析子串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591603/

相关文章:

java - 从扫描仪获取字符输入

c++ - 日期格式操作(YYYY/MM/DD-to-MM/DD/YYYY)

java - 构造函数 Adapter_PagerAdapter(FragmentManager, List<Fragment>) 未定义

list - 如何在 OCaml 中将字符列表转换为字符串?

c++ - 如何制作 char 的拷贝而不是 C++ 中的引用

检查缓冲区中的子字符串

java - 正则表达式从复杂字符串中提取子字符串

java - IntelliJ IDEA:用于String,System和其他Java类的 “cannot resolve symbol”

java - 将 Quartz 调度程序置于待机模式

java - 当用户在文本字段中输入 Java Swing 时屏蔽 IP 地址