1

I am trying to check my pair against a unit test but the output from the function returns the pair with pair values separated with "=" instead of ",". I tried type casting and other workarounds but can't figure out whats making this happen.

Function return type :

Pair<ArrayList<Pair<Entity, Integer>>, Integer>

ArrayList<Pair<Entity, Integer>> pairMatches = new ArrayList<Pair<Entity, Integer>>();

pairMatches.add(new Pair<>(entities.get(i-1), ped)); // entities is a class object

// Finally returning

return (new Pair<>(pairMatches, pedComputations)); // pedComputations and ped are Integer

Output reads : [Entity(name="frei", score=3, description="a word")=0]=1

Expected : ([(Entity(name="frei", score=3, description="a word"), 0)], 1)

@Override
public String toString() {
  return ("Entity(name=\"" + this.name + "\", score=" + this.score 
           + ", description=\"" + this.description + "\")");
}

Thank you !

1 Answers1

1

The problem lies within Pair class as you need to override toString() function in that class.

For example this code:

Pair<Integer, Integer> p = new Pair(1,2);
System.out.println(p);

will result in 1=2 being outputted.

You can either extend it the Pair class and just override toString() as your using javafx.util.Pair or create your own Pair class from scratch and override toString() there.

Example code on creating your own tuple can be found here

A.A
  • 743
  • 1
  • 8
  • 20
  • @D M - Thank you, i finally did it the way you said, thanks for the advice. – str. 0b11000 Nov 27 '17 at 21:35
  • @PeteRene I'm not DM unless I missed something, but make sure you accept the answer that helped you via the green tick on the left – A.A Nov 27 '17 at 21:43
  • @PeteRene learn [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Youcef LAIDANI Nov 28 '17 at 07:37