Reputation: 610
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.
Upvotes: 0
Views: 80
Reputation: 691635
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.
Upvotes: 3