0

Here is the code:

public class Test {
    public static void main(String argv[]) {
        String original="r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)";
        StringTest strtest=new StringTest();
        strtest.setX(original.replace("r(1,2‚0)", "TEST"));
        System.out.println(original.replace("r(1,2,0)", "TEST"));
        System.out.println(strtest);
    }
}

class StringTest {
    String x=null;

    public StringTest() {}

    public String toString() {
        return x;
    }

    public void setX(String other) {
        x=other;
    }
}

Running this gives output:

r(1,2,1)=TEST+r(1,1,0)r(1,1,0)*TEST
r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)

Why is x in strtest equal to original string and r(1,2,0) is not replaced and how do I fix it?

EDIT:

And why doesnt it work in this code, grep couldnt find any non-ascii symbols in this file:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.Arrays;
import java.io.*;

public class DFK {
    static ArrayList<Path> done = new ArrayList<Path>();

    public static void print() {
        for(Path i: done) {
            //System.out.println(i+"  "+i.isConverted());
            if(i.isConverted()) {
                System.out.println(i.getConverted());
            }
        }
    }

    public static void develop() {
        if(done.get(0).getKP1() > 0) {
            DFK.add(new Path(done.get(0).getP(), done.get(0).getQ(), done.get(0).getK()));
            DFK.add(new Path(done.get(0).getP(), done.get(0).getKP1(), done.get(0).getK()));
            DFK.add(new Path(done.get(0).getKP1(), done.get(0).getKP1(), done.get(0).getK()));
            DFK.add(new Path(done.get(0).getKP1(), done.get(0).getQ(), done.get(0).getK()));
        }
    }

    public static void add(Path x) {
            boolean exists = (done.indexOf(x)==-1 ? false : true);
            if(exists == false) {
                done.add(x);
                if(x.getKP1() >= 2) {
                    DFK.add(new Path(x.getP(), x.getQ(), x.getK()));
                    DFK.add(new Path(x.getP(), x.getKP1(), x.getK()));
                    DFK.add(new Path(x.getKP1(), x.getKP1(), x.getK()));
                    DFK.add(new Path(x.getKP1(), x.getQ(), x.getK()));
                }
            }
    }

    public static void main(String argv[]) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int p = 0, q = 0, kp1 = 0;
        Scanner in = new Scanner(System.in);
        System.out.print("p = ");p = in.nextInt();
        System.out.print("q = ");q = in.nextInt();
        System.out.print("k+1 = ");kp1 = in.nextInt();

        System.out.print("\n");
        String rkzero[][] = new String[q][q];
        for(int i=0; i<q ; i++) {
            for(int j=0; j<q ; j++) {
                System.out.print(String.format("r(%d,%d,0): ", i+1, j+1));
                rkzero[i][j]=input.readLine();
            }
        }

        done.add(new Path(p, q, kp1));
        DFK.develop();
        Collections.sort(done);

        for(int z=0; z<q ; z++) {
            for(int j=0; j<q ; j++) {
                for(Path i: done) {
                        String reg = String.format("r(%d,%d,0)",z+1,j+1);
                        i.setConverted(i.toString().replace( reg , rkzero[z][j])); //HERE IS THE PROBLEM
                }
            }
        }
        System.out.print("\n");
        DFK.print();
    }
}

class Path implements Comparable<Path> {
    int p,q,kplus1,k;
    String converted = null;

    public int getP() {
        return p;
    }

    public int getQ() {
        return q;
    }

    public int getKP1() {
        return kplus1;
    }

    public int getK() {
        return k;
    }

    public Path(int a, int b, int c) {
        super();
        p = a;
        q = b;
        kplus1 = c;
        k = c-1;
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (!Path.class.isAssignableFrom(obj.getClass())) {
            return false;
        }

        final Path other = (Path) obj;
        if(other.p == this.p && other.q == this.q && other.kplus1 == this.kplus1)
            return true;
        return false;
    }

    public int compareTo(Path other) {
        return other.kplus1-kplus1;
    }

    public String toString() {
        return String.format("r(%d,%d,%d)=r(%d,%d,%d)+r(%d,%d,%d)r(%d,%d,%d)*r(%d,%d,%d)",p,q,kplus1,p,q,k,p,kplus1,k,kplus1,kplus1,k,kplus1,q,k);
    }

    public void setConverted(String x) {
        converted = new String(x);
    }

    public String getConverted() {
        return converted;
    }

    public boolean isConverted() {
        if(converted == null)
            return false;
        return true;
    }
}

This is the output:

p = 1
q = 2
k+1 = 2

r(1,1,0): 0
r(1,2,0): 1
r(2,1,0): 1
r(2,2,0): 0

r(1,2,2)=r(1,2,1)+r(1,2,1)r(2,2,1)*r(2,2,1)
r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)
r(2,2,1)=0+r(2,1,0)r(1,1,0)*r(1,2,0)

It replaces only r(2,2,0) with 0 and nothing else, I replicated previous problem from this, but I can't find any non-asci symbols in this one.

4
  • 3
    You should take more care when using your keyboard. The second comma between 2 and 0 in the first replace call is not the usual comma. Just rewrite that and it should work. Close to vote this question as typo. Commented May 7, 2017 at 23:46
  • How did this happen, what other comma did I use Commented May 7, 2017 at 23:48
  • @Nebeski your original question was answered, and it's almost being closed. If you have another question, please ask it in another, separate question. Commented May 8, 2017 at 7:49
  • @JBNizet I posted a new question, if you could help Commented May 8, 2017 at 16:17

1 Answer 1

3

The last comma in your first "r(1,2‚0)" string is not a standard comma (with decimal value 44). Instead, it's another special character, but which looks exactly the same, with decimal value 8218. Just erase that character and replace it with a comma, and it will work as expected.

An easy way to detect such bizarre characters is to print their decimal value. For example:

String target = "r(1,2‚0)";
System.out.println(Arrays.toString(target.chars().toArray()));

which prints

[114, 40, 49, 44, 50, 8218, 48, 41]

You can clearly see here that the problematic character is out of the range of basic ASCII characters.

Sign up to request clarification or add additional context in comments.

9 Comments

Typo -> close or do you assume someone else with this same typo will find this question?
@Tom I feel that this question would mislead others with its title.
@JacobG. This whole question is not useful for somebody else and therefore doesn't belong to Stack Overflow.
If we only answered questions that could help other people, there are a whole lot of questions we would not answer. That's not a valid criteriion to answer a question or not. And I would not qualify this as a typo.
I know what you mean with "we" and these people answer every question they see, I know that, but that's not the purpose of Stack Overflow. And if you think it isn't a typo, then I wonder what your suggestion is how that char got there. Do you assume OP copies his commas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.