Results 1 to 3 of 3
  1. #1
    babe20042004 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    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


    Java Code:
    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; 03-16-2010 at 02:53 AM. Reason: Corrections

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What's the error you comes with? Missing main method or something? Because in your class main method cannot find so that you're not able to run your code.


  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't use arrays in the Book class itself as it doesn't make sense. Instead have each Book object hold the information that relates to one book, and then in your main method or elsewhere, use an array of Book objects.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  3. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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