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

Thread: Using set/get methods and arrays

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

    Default Using set/get methods and arrays

    Hi guys, forgive me if breaking all sorts of laws in Java, I'm only a couple weeks into learning it and I've been struggling the whole way through. Right now what I was trying to do was create an array using the titles of five books and to test to see if my approach was working I tried to output the result. It seems like there's an error in my set/get methods. The code compiles, but my output is null.

    So my real question is, can I do what I'm trying to achieve? That is, pull array variables from set/get methods? Is there a better approach or would this work (note: I'm supposed to have get/set methods so those have to stay, I'm struggling with my constructors and arrays at the moment) I'll be working with this some more while I wait for a response so if I figure it out first I'll update with a solved tag and the code I end up using to do so, thanks!

    import java.util.Arrays;
     
    public class BookStore2 {
     
     
        public static void main(String[] args) {
     
            Book Book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f);
            Book Book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
            Book Book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
            Book Book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f);
            Book Book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f);
     
            String[] titleArray = new String[] {Book1.gettitle, Book2.gettitle, Book3.gettitle, Book4.gettitle, Book5.gettitle}; 
            System.out.println(Arrays.toString(titleArray));
     
        }
     
     
    }
     
     
    class Book{
     
      String ISBN;
      String title;
      String name;
      int year;
      String publisher;
      float price;
        String gettitle; // removed, this must have been part of the problem I was having
     
      public Book(String ISBN, String title, String name, int year, String publisher, float price){
          this.ISBN = ISBN;
          this.title = title;
          this.name = name;
          this.year = year;
          this.publisher = publisher;
          this.price = price;
      }
       public void setISBN(String ISBN){
           this.ISBN = ISBN;
       }
       String getISBN(){
           return ISBN;
       }
       public void settitle(String title){
           this.title = title;
       }
       String gettitle(){
           return title;
       }
       public void setname(String name){
           this.name = name;
       }
       String getname(){
           return name;
       }
       public void setyear(int year){
           this.year = year;
       }
       int getyear(){
           return year;
       }
       public void setpublisher(String publisher){
           this.publisher = publisher;
       }
      String getpublisher(){
          return publisher;
      }
       public void setprice(float price){
           this.price = price;
       }
       float getprice(){
           return price;
       }
       // list of getters
     
     
    } // end class book

    Updated code:

    import java.util.Arrays;
     
    public class BookStore2 {
     
     
        public static void main(String[] args) {
     
            Book book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f);
            Book book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
            Book book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
            Book book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f);
            Book book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f);
     
            String[] titleArray = new String[] {book1.gettitle(), book2.gettitle(), book3.gettitle(), book4.gettitle(), book5.gettitle()}; 
            System.out.println(Arrays.toString(titleArray));
            System.out.println(book1.gettitle());
     
        }
     
     
    }
     
     
    class Book{
     
      String ISBN;
      String title;
      String name;
      int year;
      String publisher;
      float price;
     
     
      public Book(String ISBN, String title, String name, int year, String publisher, float price){
          this.ISBN = ISBN;
          this.title = title;
          this.name = name;
          this.year = year;
          this.publisher = publisher;
          this.price = price;
      }
       public void setISBN(String ISBN){
           this.ISBN = ISBN;
       }
       String getISBN(){
           return ISBN;
       }
         String gettitle(){
           return title;
       }
       public void settitle(String title){
           this.title = title;
       }
       public void setname(String name){
           this.name = name;
       }
       String getname(){
           return name;
       }
       public void setyear(int year){
           this.year = year;
       }
       int getyear(){
           return year;
       }
       public void setpublisher(String publisher){
           this.publisher = publisher;
       }
      String getpublisher(){
          return publisher;
      }
       public void setprice(float price){
           this.price = price;
       }
       float getprice(){
           return price;
       }
       // list of getters
     
     
    } // end class book


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using set/get methods and arrays

    my output is null.
    What variable are you printing that has a null value? Where do you give that variable a value so that it is not null? If you don't assign it a value it will keep the null default value.

    It is confusing to name a variable and a method with the same exact name. Can you change one of the names to make them unique?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using set/get methods and arrays

    Okay, I found part of my problem, instead of writing this code:

     String[] titleArray = new String[] {Book1.gettitle, Book2.gettitle, Book3.gettitle, Book4.gettitle, Book5.gettitle}; 
            System.out.println(Arrays.toString(titleArray));
            System.out.println(Book1.gettitle);
    I changed it to:

        String[] titleArray = new String[] {Book1.title, Book2.title, Book3.title, Book4.title, Book5.title}; 
            System.out.println(Arrays.toString(titleArray));
            System.out.println(Book1.title);

    To answer your questions Norm, the variable I was trying to get to print is the title variable from my Book class. The variables should be assigned using the constructors that you see for Book1 - Book5.

    As for the second part of your question, I am uncertain what you mean by a variable and method with the same name maybe I am reading it wrong or understanding it wrong, but they appear to be all with different names?

    With the exclusion of, I found a duplicate gettitle line in my code near the beginning of the Book class, I removed that as I assume it was part of the problem.

    Edit: I see what you mean, you meant the variable gettitle and the method gettitle. Yeah, the variable for gettitle was not supposed to be there, it was removed once I found that it was there and then correcting the code to title instead of gettitle resulted in the correct output.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using set/get methods and arrays

    a variable and method with the same name
    See gettitle: It's a String and the name of a method,

    Where does the code use any of the get methods? It should use a method (book1.getTitle()) to access a class value and not access the class's variable directly like this: Book1.title

    Note: variable names should start with lowercase letters: book1 vs Book1
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Shadud (November 25th, 2012)

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using set/get methods and arrays

    Quote Originally Posted by Norm View Post
    See gettitle: It's a String and the name of a method
    I removed the String already, as it was part of my confusion, that was the reason for producing a null value instead of a syntax error. I also made adjustments to the variable names to help readability and you pointed out the error that caused all of this to begin with. I wasn't understanding why it wasn't calling on the get methods, and it was because I left out the parenthesis (book1.gettitle()) at the end.

    I'll probably be asking for more help soon, this stuff gives me one heck of a headache.

    I will update the thread as solved, I don't think there is much that can be asked here and still remain on-topic.

    Thank you, Norm.

Similar Threads

  1. Java Class : How do I set up a toString method for arrays?
    By red7 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 11th, 2012, 05:47 PM
  2. How to use Get and Set methods
    By jessenmutta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2012, 09:05 PM
  3. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 10:10 PM
  4. How do get and set methods work??
    By merdzins in forum Object Oriented Programming
    Replies: 2
    Last Post: December 25th, 2010, 03:17 AM
  5. Should validation code exist in set methods or in a validate method
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 4
    Last Post: May 27th, 2010, 08:37 AM