Results 1 to 19 of 19
  1. #1
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Trouble Returning Data Usng Set/Get Methods

    Hello, I'm writing a program that involves the use of set/get methods. I will submit a sample code of my issue. The format appears to be alright however the program is not returning any data. This is the same format as my actual code. Does anybody have any idea why this might be happening?

    Java Code:
    public static void main(String [] args) {
    
    SecondClass object = new SecondClass();
    object.setName("Name");
    
    object.getName();
    
    }
    Java Code:
    public class SecondClass {
    
    private String name;
    
    public SecondClass(){
    
    name = " ";
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    public String getName() {
    return name;
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trouble Returning Data Usng Set/Get Methods

    It returns it. But you didn't assign the return value to anything. You need to print out what the method returns.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Thanks a lot Jim. So just to be clear, the return data, whatever it is (String, int, etc) does not actually print in the console--instead we use the get method to assign that return value to a variable in the class calling the method?

  4. #4
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Also, do you believe it is best to assign that value to a variable within the class calling the method, or use a sys.out within the get method for cleaner code layout?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Quote Originally Posted by the_dude View Post
    Thanks a lot Jim. So just to be clear, the return data, whatever it is (String, int, etc) does not actually print in the console--instead we use the get method to assign that return value to a variable in the class calling the method?
    Correct! And whether you do this:

    Java Code:
    System.out.println(object.getName());
    or this:

    Java Code:
    String retVal = object.getName();
    System.out.println(retVal);
    Is a matter of taste and what you are doing at the time. I use both depending on the situation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    AAChaoshand's Avatar
    AAChaoshand is offline Member
    Join Date
    Jan 2015
    Location
    Texas
    Posts
    22
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    This is something I was messing with today since I had learned how to pull and put from other classes. Good to see it get reinforced by your post. It puts the concept into a better perspective for me. Trying to dream in code while I sleep so the last thing I do before going to bed is finish up something small in java to keep me going. "this" is the next thing we are supposed to cover in the next tutorial the guy has online, he said it was confusing to get your head around but seeing this doesn't make me think it will be.

  7. #7
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Excellent! Thanks a lot for clarifying this for me. I suppose I reasoned that each get method returned the specific data type that would print (return) that information to the console.

  8. #8
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    I would like one of five other parameters of my object to be a string array. Every other parameter is either of a single String or int data type. When I printed the data of the object (each of the 5 parameters), the array is printing out the memory location of the data, but not the actual data -- at least i believe it is the memory location. How might I display all the data, each parameter printed on its own line in the console? In other words, I want each String in the array to print on the same line with a space between it.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Trouble Returning Data Usng Set/Get Methods

    want each String in the array to print on the same line with a space between it.
    use a loop to access the elements and the print() method to print them.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Ok, I've tried that but it does not work.
    Sample format:

    //Second Class
    public void setStars(String [] stars){
    for(int i = 0; i < stars.length; i++) {
    this.stars = stars;
    }
    }

    public String[] displayStar() {
    do{
    return stars;
    } while(stars != null);
    }

    //Main Class
    String [] stars1 = new String [] {"One", "Two"};

    System.out.print("Star: ");
    for(int i = 0; i < star.length; i++){
    System.out.print(theater1.displayStars());
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Does that even compile? Where is displayStars()? And your displayStar method is wrong in any event. You don't even need it.
    Just loop thru your String array and print the values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Hey Jim,

    This is just a small sample of the actual code. As per the assignment, we are supposed to use accessor methods. It was my intention to setup the array and pass this through the object (a movie) that has a certain number of characteristics, which are part of the overloaded constructor. The program I do have runs in full, except the array only prints out the following: Starring: [Ljava.lang.String;@618787c9[Ljava.lang.String;@618787c9

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Trouble Returning Data Usng Set/Get Methods

    [Ljava.lang.String;@618787c9
    That is the String returned by an array's toString() method. The compiler generates the call to the toString() method when an array is used in a String concatenation. To get the contents of an array, use the index notation: [index]
    after the name of the array. For example: theArray[0]
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Thanks Norm, but how would I be able implement what you are saying using the accessor methods?

    What I've done is pass the array as part of the object constructor and I would like to access and display the contents of the array via the get method (displayStar()).

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Trouble Returning Data Usng Set/Get Methods

    If you want to access the array with an accessor method, then just return the array. And use the return value as the target of a for loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Forgive me for being dense here, but I'm not getting the result I need.

    public String[] displayStar() {
    return stars;
    }

    Is returning the output I mentioned earlier: [Ljava.lang.String;@15db9742
    Now, I know Norm mentioned that I need to use index notation, but I tried using an enhanced for statement to print out the entire String array, but now it just prints out multiple [Ljava.lang.String;@15db9742 messages. I tried to use a for statement within the displayStar method, but got nowhere with that.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Trouble Returning Data Usng Set/Get Methods

    [Ljava.lang.String;@15db9742
    That is the String returned by the toString() method of an array of String.
    One way to print the contents of an array is to use the Arrays class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    The displayStar() method returns a String array. (Doubtful name for a method that returns a value. I'd expect it to display something)
    The code that calls the method must be ready to handle the String array that is returned.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Yes, I came across one of your other posts that stated the same. Without using the util.Arrays,and given the last comment of your post, should it not return the String format of the array data by using the for loop?

    ex:
    for(String str : stars)
    System.out.print(theater1.displayStars() + " " );

    Another question: if I were to allow the user to enter any number of stars as a parameter for setStars (i.e. setStars(a,b,c,...etc) ). Is it possible to then add the parameters (String in this case) to a new array in the other class? In other words, use setStars to build arrays using the method parameters. Our assignment says that we should use set methods to input the data for each movie, but it seems excessive to build an array in the main class and pass it to the other class, only to pass it back to the main class via accessor methods.
    Last edited by the_dude; 01-29-2015 at 01:15 AM.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Trouble Returning Data Usng Set/Get Methods

    Java Code:
    for(String str : stars)
       System.out.print(theater1.displayStars() + " " );
    That loop doesn't use the value in the str variable for anything. The contents of the stars array controls the number of times the loop goes around.
    return the String format of the array data by
    That loop does not return anything or create anything that could be returned by a return statement.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 11
    Last Post: 09-21-2012, 09:33 AM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Help with Value Returning Methods
    By SoCal.SD in forum New To Java
    Replies: 5
    Last Post: 02-27-2011, 05:58 AM
  4. Replies: 11
    Last Post: 12-08-2008, 12:29 AM
  5. Replies: 5
    Last Post: 07-18-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •