Question

I have this line of code:

    System.out.print(postalCodeIndex.findClosestBruteForce(latitude, longitude));

It returns output from a text file that was ran through an algorithm. For example the output is can be : "A0E 2Z0 Monkstown Newfoundland NL D [47.150300:-55.299500]". I would like to convert that output to a string so I can use it in a javafx GUI text. Is that posible?

Was it helpful?

Solution

System.out.print accepts a String as a parameter, in fact, anything that you send it will be converted to a String in order for it to be displayed.

Using the following code, you could then put the result of the postalCodeIndex method call into a variable called myString.

String myString = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();

It might be worth your while remembering that the process in the System.out.print() code sample works as follows:

  1. postalCodeIndex is called FIRST, creating a temporary String in-place because the .toString() method is called on your behalf.
  2. The System.out.print method is only called AFTER the postalCodeIndex method has returned, because System.out.print requires this returned String to enable it to print something to the console.

OTHER TIPS

postalCodeIndex.findClosestBruteForce(latitude, longitude)

this method it self would returning a String or if not you can do like

String str = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();

Based on the code you've given, the code inside the System.out.print() call will return a PostalCode object. So to get a string you could do something like:

String x = postalCodeIndex.findClosestBruteForce(latitude, longitude).toString();
//use x as String
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top