String .replace() not applicable

5k views Asked by At

I am trying to remove the non-number characters from my string.

I have tried using the .replace() method but this returns with the error:

The method replace(char, char) in the type String is not applicable for the arguments (String, String)

Code:

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);
}

When I print out the string gg, initialized like:

String gg = p.toString();

I get the output: Object020f458.

My computer highlights the error on the replace:

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

There are 5 answers

0
TotallyNotSuspisousAccount On BEST ANSWER

Since the String I was using was actually using was a property I had to retrieve it like a property which will format it itself

 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));

   }

I also had to change my text file to match the property format:

Number_of_properties = 4
x1 = 150
y1 = 70
x2 = 55
y2 = 77
1
Aniket Goel On

You can use

public String replaceAll(String regex, String replacement)

This will replace all matching expressions with the replacements.

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

So I was having a look at NXT and its APIs (JavaDocs)
Btw, I am by no mean an expert on NXT and leJOS, I'm guessing.

You can see by the JavaDoc, that the replace(CharSequence, CharSequence) method is not present.

enter image description here

While I wouldn't solve the problem in such a way, you can try using a StringBuilder to remove the unwanted chars.

See for example a Jon Skeet answer for ideas https://stackoverflow.com/a/3472705/1392277

You can extract a method such as:

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
Suraj Z On

The error must have been highlighted on below two line and not for replace function

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

you forgot to put comma in second line. It should be

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

Still it wont work because you are trying substring function with invalid range (as value of commaLoc is -1).

Try replacing those last two lines as below just to make it error free.

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

problem:

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

error message:

The method replace(char, char) in the type String is not applicable for the arguments (String, String)

Please try to replace with Character.MIN_VALUE:

Replace " with ', then replace all '' (, since java "doesn't like the empty character literal") with Character.MIN_VALUE):

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

Character.MIN_VALUE is not empty character, but closest to it :), and converts (with a String.replcae(char,char) test):

{foo=bar}

to:

\u0000foo\u0000bar\u0000

...which appears hard to copy&paste, but "looks like blanks" :)