Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with return value.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    8
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    // 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;
    	}
    }
    Last edited by E.K.Virtanen; March 27th, 2013 at 12:45 PM. Reason: Solved.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Problem with return value.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Problem with return value.

    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.

    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.

    class Piste {
     
        ...
     
        public String toString() {
            // put some code here...
            return "the string you want";
        }
    }

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    8
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with return value.

    Thank you both with this. After pondering your replies and my code, i got it figured out.

Similar Threads

  1. [SOLVED] Problem with return and 2D array
    By J-moges in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2013, 11:53 PM
  2. Having java return method problem
    By jayleongwk in forum Java Theory & Questions
    Replies: 2
    Last Post: November 4th, 2012, 02:09 AM
  3. Switch problem. How do I return to my menu?
    By jonny007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2012, 11:54 AM
  4. TemperatureConverter Return Problem
    By ShadowKing98 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2011, 11:17 AM
  5. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM