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: My Array. Help Please

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default My Array. Help Please

    The no-arg constructor is supposed to be there. This is my appended code. The user should be able to input the amount of data to fill the fields how many times they want. They should also be able to view the data that they entered. Here is the appended code. Thanks

    class Book {
        int num;
        static String isbn;
        static String author;
        static String title;
        static double price;
        static int quant;  
     
     
        public Book() {
    	num=0;
            isbn="XXXXXX";
            author="No Author";
            title="No Title";
            price=0;
            quant=0;
        }
        public Book(String isbn, String author, String title, double price, int quant) {
            Book.isbn = isbn;
            Book.author = author;
            Book.title = title;
            Book.price = price;
            Book.quant = quant;
        }
        public String toString() {
            String s = "ISBN: " + isbn;
            s += "\nAuthor: " + author;
            s += "\nTitle: " + title;
            s += "\nPrice: " + price;
            s += "\nQty in Stock: " + quant ;
     
            return s;
        }
    }
     
    public class Bookstore {
     
       static int num=0;
     
     
        public static void main( String [] args) {
            Book books[] = new Book;
            java.util.Scanner sc = new java.util.Scanner( System.in );
     
            System.out.println("How many books would you like to record?");
            num=sc.nextInt();
     
            for (int i = 0; i < num; i++) {
     
                System.out.println("Book no. " + i + 1);
                System.out.print("Enter ISBN: ");
                Book.isbn = sc.nextLine();
                System.out.print("Enter author: ");
                Book.author = sc.nextLine();
                System.out.print("Enter title: ");
                Book.title = sc.nextLine();
                System.out.print("Enter price: ");
                Book.price = sc.nextInt();
                System.out.print("Enter quantity: ? ");
                Book.quant = sc.nextInt();
     
                    } 
     
     
     
     
     
                for (int i = 0; i < num; i++) {
     
                    System.out.println( books[ i ] );
     
                }
        }
    }
    Last edited by babe20042004; March 15th, 2010 at 09:20 PM. Reason: Corrections


  2. #2
    Junior Member switcha's Avatar
    Join Date
    Sep 2011
    Location
    Australia
    Posts
    20
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: My Array. Help Please

    I have several questions for you. First of all, class book looks like an entity class, so why not use set and get methods for data access and retrieval? This ensures proper data encapsulation and adheres to java best practices.It also enables you to retreive more specific attribute values. Second, to me it looks like you are using num as an ID, so why not rename the field ID?
    If your intention is to use num as a count for the total number of book objects created, you can always declare static field count that is incremented in both constructors. You can then use the variable count to auto-increase the ID field and only set a retrieval or 'get' method to access this field.
    My next question focuses on the use of your bookstore. Why are you using an array? Wouldn't it be easier to use a generic books collection to represent the many-to-one relationship between books and bookstore i.e. many books are found in one bookstore? I assume this is the relationship you are trying to model. You can add individual books to the collection bookstore and iterate through them to access/print the details.
    Generally, arrays are fixed length i.e. they have a predetermined value, whereas collections are dynamic and re-sizable and used when you do not know the total number of objects being modeled - in this case, individual books. One of the reasons why you will be having difficulty (besides the above) is for starters, you create the array books - but in your for loop, you do not assign books[i] any new values to reference. So even though you've created the array type of books, you haven't assigned any books to it - thus you are referencing objects that are assigned a null value. You need to add code similar to the following:
    for ( int i = 0; i < books.length; i++ )
            {
               books[i]= new Book();

    ...let me know your thoughts before I go on.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: My Array. Help Please

    ...let me know your thoughts before I go on.
    Hey Switcha, just a quick note that I'd bet the original poster has solved or moved on from this problem, considering the initial post date is over a year old

  4. #4
    Junior Member switcha's Avatar
    Join Date
    Sep 2011
    Location
    Australia
    Posts
    20
    My Mood
    Fine
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: My Array. Help Please

    ...Good point. Ahwell, still good to practice on other people's prob's - it keeps the mental tool sharp

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 11:44 AM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM