【问题标题】:Parse a substring?解析子字符串?
【发布时间】:2011-11-27 08:13:03
【问题描述】:

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

【问题讨论】:

  • 这看起来/应该/有效。您是否收到特定的编译器/运行时错误?
  • 定义“不能”...当然还要发布实际代码来演示问题。
  • 这是java程序,很臭

标签: java char substring parseint


【解决方案1】:

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

但这是一个您应该使用带断点的调试器的区域,这样您就可以确定变量中放置了哪些值,或者只是将它们打印出来:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多