Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Java replace not working as expected

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.

Answer*

Draft saved
Draft discarded
Cancel
9
  • Typo -> close or do you assume someone else with this same typo will find this question? Commented May 7, 2017 at 23:49
  • 1
    @Tom I feel that this question would mislead others with its title. Commented May 7, 2017 at 23:50
  • @JacobG. This whole question is not useful for somebody else and therefore doesn't belong to Stack Overflow. Commented May 7, 2017 at 23:51
  • 2
    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. Commented May 7, 2017 at 23:52
  • 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? Commented May 7, 2017 at 23:56

lang-java

AltStyle によって変換されたページ (->オリジナル) /