0

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));
gre_gor
  • 6,669
  • 9
  • 47
  • 52

5 Answers5

2

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();        
}
LppEdd
  • 20,274
  • 11
  • 84
  • 139
1

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" :)

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • ...but this is not your actual problem. – xerx593 Feb 10 '19 at 19:11
  • 1
    I think a StringBuilder solution is better suited. MIN_VALUE is not really removing the char, as you said – LppEdd Feb 10 '19 at 19:11
  • 1
    ...sure! this is a *total hack* approach :) ....but still confusing, why `String.replace("{", "")` not works. – xerx593 Feb 10 '19 at 19:14
  • 1
    See my answer above. Most probably he is using a "proprietary/custom" JDK API. – LppEdd Feb 10 '19 at 19:15
  • ahmmhk, i got "api" and "lego", but i didnt realize it's a "a custom jdk". You are right, better solution: StringBuilder/JohnSkeet always good! :) – xerx593 Feb 10 '19 at 19:22
  • 1
    I'm not really sure on the custom JDK, but I mean, it's on Lego and probably they've put some restriction on the amount of usable APIs. Felt strange reading Jon Skeet comment on this question :) – LppEdd Feb 10 '19 at 19:23
  • but i leave this post, because (i found nothing) no one tried/ posted yet: `String.replace(char, Character.MIN_VALUE)` :) – xerx593 Feb 10 '19 at 19:23
  • 1
    thx, bro! :) ..at least [StringBuilder](http://www.lejos.org/nxt/nxj/api/java/lang/StringBuilder.html) *is in* that jdk! pheew! ..but still I think OP has to solve the "`Properties` problem", too. – xerx593 Feb 10 '19 at 19:29
  • 1
    As many SO questions, this one has business-logic + technical problems. I think that if the OP takes a board and begins drawing a diagram/flow-chart of the domain problem, this thing won't even be required. – LppEdd Feb 10 '19 at 19:33
  • 1
    Found, VM name is https://en.wikipedia.org/wiki/TinyVM See also https://en.wikipedia.org/wiki/LeJOS#NXJ_and_the_Java_platform – LppEdd Feb 10 '19 at 20:39
0

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

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
    That will throw an exception in normal Java, because `replaceAll` accepts a regular expression pattern. You'd need `"\\{` instead of "{". And it looks like `replaceAll` isn't available in this environment either. – Jon Skeet Feb 10 '19 at 18:06
-1

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));
Suraj Z
  • 1
  • 5
  • Thanks for advice. What I did is debug the entire code on Java 1.8, however I found the issue in mentioned lines. And not replace function – Suraj Z Feb 11 '19 at 03:15