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 3 of 3

Thread: Having java return method problem

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having java return method problem

    getFullAddress() should return the address as a formatted String, one item per line:
    e.g. 23 High Street
    Newcastle
    NE1 1NE // There should be a space between the two parts of the postcode.

    how i can get done of this return?

    public class Address
    {
        private String postcode;
        private String street;
        private String town;
     
        /**
         * Constructor for objects of class Address.
         *
         *@param street The Address's Street
         *@param town The Address's Town
         *@param postcode The Address's Postcode
         */
        public Address (String street , String town , String postcode)
        {
            this.street = street;
            this.town = town;
            this.postcode = postcode;
        }
     
        // accessors
     
        /**
         * Get the Address's FullAddress
         * 
         * @return the address as a formatted String,  one item per line
         */
        public String getFullAddress()
        {
            String fullAddress = "street/n" + "town/n" + "postcode";
            return fullAddress;
        }
     
        /**
         * Get the Address's Postcode
         * 
         * @return the Address's Postcode
         */
        public String getPostcode()
        {
            return postcode;
        }
     
        /**
         * Get the Address's Street
         * 
         * @return the Address's Street
         */
        public String getStreet()
        {
            return street;
        }
     
        /**
         * Get the Address's Town
         * 
         * @return the Address's Town
         */
        public String getTown()
        {
            return town;
        }
     
        /**
         * Print out the Address to the console window
         * 
         */
        public void printAddress()
        {
            System.out.println(street + "\n" + town + "\n" + postcode);
        }
     
        //mutators
     
        /**
         * Change the Address's street,town and postcode
         * 
         * @param street the street
         * @param town the town
         * @param postcode the postcode
         */
        public void setFullAddress (String street , String town , String postcode)
        {
            this.street = street ;
            this.town = town;
            this.postcode = postcode;
        }
     
        /**
         * Change the Address's postcode
         * 
         * @param postcode the postcode
         */
        public void setPostcode (String postcode)
        {
            this.postcode = postcode;
        }
     
        /**
         * Change the Address's street
         * 
         * @param postcode the street
         */
        public void setStreet (String street)
        {
            this.street = street;
        }
     
        /**
         * Change the Address's town
         * 
         * @param postcode the town
         */
        public void setTown (String town)
        {
            this.town = town;
        }
     
        //End Class
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Having java return method problem

    Quote Originally Posted by jayleongwk View Post
    NE1 1NE // There should be a space between the two parts of the postcode.
    The problem is "street/n" as well as "town/n" it should be "street\n" and "town\n"! That's why you don't get any spacing!

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Having java return method problem

    Regarding:
        public String getFullAddress()
        {
            String fullAddress = "street/n" + "town/n" + "postcode";
            return fullAddress;
        }

    you appear to be returning hard-coded Strings and not the data held by the object of this class. Instead of "street/n" why not call getStreet() + "\n" or street + "\n" (no quotes around street), and the same for the other items.

Similar Threads

  1. [SOLVED] using the return of a method to initialize an ArrayList
    By mia_tech in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 06:05 PM
  2. [SOLVED] Return all values in an ArrayList from a method
    By IanSawyer in forum Collections and Generics
    Replies: 1
    Last Post: March 28th, 2012, 10:53 AM
  3. Why I can't return two variables in one method?
    By netlync in forum Java Theory & Questions
    Replies: 3
    Last Post: January 13th, 2012, 02:39 AM
  4. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 03:38 PM
  5. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 02:56 AM