字符串.replace()不适用

时间:2019-02-10 17:41:07

标签: java nxt

我正在尝试从我的字符串中删除非数字字符。

我尝试使用.replace()方法,但这返回错误:

  

字符串类型中的方法replace(char,char)不适用于参数(String,String)

代码:

Properties p = new Properties();
File f = new File("coords.txt");
if (f.exists()) {
    FileInputStream in;
    try {
        in = new FileInputStream(f);
        p.load(in);
        in.close();
    } catch (Exception e) {
        System.err.println("Failed to load coordinates");
        System.err.println(e.getMessage());
        Button.waitForAnyPress();
        System.exit(0);
    }
} else {
    System.out.println("No coordinates found");
    while (!Button.ESCAPE.isPressed()) {
        Thread.yield();
    }
    System.exit(0);
}

当我打印出字符串gg时,初始化如下:

String gg = p.toString();

我得到输出:Object020f458

我的计算机突出显示了替换错误:

gg = gg.replace("{", "");
gg = gg.replace("=", "");
gg = gg.replace("}", "");

int commaLoc = gg.indexOf(",");
int x = Integer.parseInt(gg.substring(0,commaLoc));
int y = Integer.parseInt(gg.substring(commaLoc + 1));

5 个答案:

答案 0 :(得分:2)

因此,我正在研究NXT及其API(JavaDocs
顺便说一句,我猜我绝对不是NXT和leJOS的专家。

您可以通过JavaDoc看到replace(CharSequence, CharSequence)方法不存在。

enter image description here

虽然我不能以这种方式解决问题,但是您可以尝试使用StringBuilder删除不需要的字符。

例如,请查看乔恩·斯基特(Jon Skeet)的答案 https://stackoverflow.com/a/3472705/1392277

您可以提取一种方法,例如:

private String removeChars(
        final String originalString,
        final String charsToRemove) {
    final StringBuilder stringBuilder = new StringBuilder(originalString);
    int i = stringBuilder.indexOf(charsToRemove);

    while (i != -1) {
        stringBuilder.replace(i, i + charsToRemove.length(), "");
        i = stringBuilder.indexOf(charsToRemove, i);
    }

    return stringBuilder.toString();        
}

答案 1 :(得分:1)

问题:

gg = gg.replace("{", "");
gg = gg.replace("=", "");
gg = gg.replace("}", "");

错误消息:

  

String类型的方法replace(char,char)不适用于参数(String,String)

请尝试替换为Character.MIN_VALUE

"替换为',然后用''替换所有Character.MIN_VALUE(因为Java“不喜欢空字符文字”):

gg = gg.replace('{', Character.MIN_VALUE);
gg = gg.replace('=', Character.MIN_VALUE);
gg = gg.replace('}', Character.MIN_VALUE);

Character.MIN_VALUE是不是空字符,但最接近它 :),然后进行转换(通过String.replcae(char,char)测试):

{foo=bar}

至:

\u0000foo\u0000bar\u0000

...似乎很难复制和粘贴,但“看起来像空白”:)

答案 2 :(得分:0)

由于我实际上使用的字符串是一个属性,因此我必须像对其进行格式化的属性一样检索它

 int Count = Integer.parseInt(p.getProperty("Number_of_properties"));
   for(int i = 1; i < Count; i++)
   {
    int x = Integer.parseInt(p.getProperty("x"+i));
    int y = Integer.parseInt(p.getProperty("y"+i));

   }

我还必须更改文本文件以匹配属性格式:

Number_of_properties = 4
x1 = 150
y1 = 70
x2 = 55
y2 = 77

答案 3 :(得分:-1)

您可以使用

public String replaceAll(String regex, String replacement)

这将用替换项替换所有匹配的表达式。

gg = gg.replaceAll("{", ""); // replace all "{" with "".
gg = gg.replaceAll("=", "");
gg = gg.replaceAll("}", "");

答案 4 :(得分:-1)

错误必须在下面两行中突出显示,并且不能用于替换功能

   int x = Integer.parseInt(gg.substring(0,commaLoc));
   int y = Integer.parseInt(gg.substring(commaLoc + 1));

您忘记将逗号放在第二行。应该是

   int y = Integer.parseInt(gg.substring(commaLoc, + 1));

它仍然无法正常工作,因为您正在尝试使用无效范围的子字符串函数(因为commaLoc的值为-1)。

尝试如下替换最后两行,以使其没有错误。

      int x = Integer.parseInt(gg.substring(6, 7));
      int y = Integer.parseInt(gg.substring(7, 8));