0

Not sure if the title explains what I really want to do, however,

I have a piece of information that currently prints out as

[[1/2, 1/2], 1]

however I need it to print out in the format

{{true@1/2, false@1/2}@1}

How would I go about doing this, I do have my toString method set up however I wasn't sure how to get the "true@" and remove the [[, I got the { however.

Currently I have,

@Override
public String toString() {
    String s = "{" + this.knowledge + "}";
    return s;

Any help would be much appreciated.

Regards,

Sim

Sim
  • 570
  • 1
  • 10
  • 22
  • 1
    You have to post more of your class and how you have overriden the toString() method – shyam Mar 22 '13 at 09:06
  • The `toString` method is used for *debugging purposes*. If you want this custom printing for an end-product, you should introduce your own functions. – Vincent van der Weele Mar 22 '13 at 09:09
  • @Heuster, or override `toString()`, exactly as the OP is doing... – jedwards Mar 22 '13 at 09:15
  • look here http://stackoverflow.com/questions/1928191/what-is-the-correct-way-of-overriding-hashcode-and-equals-methods-of-persi/10700355#10700355 – Michael Mar 22 '13 at 09:19

2 Answers2

1

I am assuming you have only access to a string (this.knowledge) of this format: [[str,str], str], in that case the following solution will work:

@Override
public String toString() {
   StringBuilder strB = new StringBuilder();
    for (int i=0; i<this.knowledge.length(); i++) {
        if(this.knowledge.charAt(i)!='[' && this.knowledge.charAt(i)!=']' && this.knowledge.charAt(i)!=',') 
           strB.append(this.knowledge.charAt(i));
    }
    String[] strs = strB.toString().split("\\s+");
    return "{{true@" + strs[0] + ", false@" + strs[1]+ "}@" + strs[2] + "}";
}
sowrov
  • 1,018
  • 10
  • 16
0

Not sure if this answer explains what you really want to do, however,

The "piece of information" you have looks a lot like the default printout you might get from a list of lists. I was able to see something "similar" with:

List<String> inner = new ArrayList<String>();
inner.add("1/2");
inner.add("1/2");

List<Object> outer = new ArrayList<Object>();
outer.add(inner);
outer.add("1");

System.out.println(outer);

If it's correct to assume that knowledge is some known collection (of collections), you could create a format method that would be able to take the known elements and put them in the correct place. If "true" and "false" are hard-coded text strings, it might look like:

public String formatKnowledge(List list) {
    return "{{true@" + ((List)list.get(0)).get(0) + ", false@" + 
        ((List)list.get(0)).get(1) + "}@" + list.get(1) + "}";
}

Now clearly this would only work for that exact type of list input. If the list could be more complex, more complex logic would be needed to create the correct text (and braces) in the proper locations. Also if "true" and "false" are calculated somehow, that logic would also need to be added.

Disclaimer: The above format method is for illustrative purposes only. The author does not condone the use of crazy blind casts and crazier blind indexing...

SeKa
  • 1,825
  • 12
  • 7