javanxtlejos-nxj

String .replace() not applicable


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

Solution

  • 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