# Problem with return value.

• March 25th, 2013, 02:27 PM
E.K.Virtanen
Problem with return value.
Hi.

Still strugling with learning objects.

Basic idea is, that object Piste(x, y) checks if parameters are in proper range and returns a string in format "(xLoc, yLoc)".
.siirrä(x, y) adds parameters to xLoc and yLoc and checks does they stay in proper range and returns a string in same format.

Return value is "Piste@998b08" and im not sure is it up to my whole code or where. If someone could pinpoint where im doing the error, i would be pleased.

Code :

```// in main code Piste piste = new Piste(1, 5); System.out.println("Pisteen paikka on nyt " + piste); piste.siirrä(10, 20); System.out.println("Pisteen paikka on nyt " + piste); //     // Here is the code im pondering with. class Piste{ int xLoc; int yLoc;   public Piste(int x, int y) { xLoc = x; yLoc = y; }   public String Piste(int x, int y) { xLoc = x; yLoc = y; if (xLoc < 0) xLoc = 0; if (yLoc < 0) yLoc = 0; if (xLoc > 100) xLoc = 100; if (yLoc > 100) yLoc = 100; return stringiksi(xLoc, yLoc); }   public String siirrä(int x, int y) { xLoc = xLoc + x; yLoc = yLoc + y; if (xLoc < 0) xLoc = 0; if (yLoc < 0) yLoc = 0; if (xLoc > 100) xLoc = 100; if (yLoc > 100) yLoc = 100; return stringiksi(xLoc, yLoc); }   public String stringiksi(int xLoc, int yLoc) { String palautus; palautus = "(" + Integer.toString(xLoc) + "," + Integer.toString(yLoc) + ")"; return palautus; } }```
• March 25th, 2013, 02:50 PM
Norm
Re: Problem with return value.
Quote:

Return value is "Piste@998b08"
That is the String that is returned by the Object class's toString() method. If you want to print the value of an instance of a class, the compiler generates a call to the class's toString() method. You can override the toString() method in your class and have it return the String you want to see.
Or you should call a method in the class that returns the String you want to print.
The code should save the String returned by the siirrä() method and print that.

BTW having a method name that is the same as the class name is confusing. method names should start with a lowercase letter.
• March 25th, 2013, 02:51 PM
pbrockway2
Re: Problem with return value.
Quote:

Code :

```public String Piste(int x, int y) { xLoc = x; ...```

It would be better to call this method something else (something that says what it does) and start the name with a lowercase letter. At the moment it looks like a constructor and that can be confusing.

Code :

`System.out.println("Pisteen paikka on nyt " + piste);`

This line will not use stringiksi() to figure out what to print. Rather it will use the method toString() that all classes (including Piste) have. To make it print something nicer you will have to write a toString() method.

Code :

```class Piste {   ...   public String toString() { // put some code here... return "the string you want"; } }```
• March 27th, 2013, 12:45 PM
E.K.Virtanen
Re: Problem with return value.
Thank you both with this. After pondering your replies and my code, i got it figured out.