Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default Creating a number of objects based on user input.

    If I were to prompt a user for the number of objects to be created, how would I go about that? Up to this point, I've created new objects on my own.

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

  3. #3
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I've used a for loop, but I believe I am running into a problem. The objects that I'm creating contain data, but I think that the data will be overwritten.

    Java Code:
    public class CustomerDemo {
    
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
    
            System.out.print("How many customers would you like to enter? ");
            while(!in.hasNextInt()){
                System.out.println("'" + in.nextLine() + "'" + " is not a valid entry. Please enter a number: ");
            }
            int customers = in.nextInt();
    
            for(int i = 1; i <= customers; i++){
                Customer customer = new Customer(i);
                System.out.println("Please enter customer " + i + " data:");
                System.out.print("First Name: ");
                String first = in.next();
    
                System.out.print("Last Name: ");
                String last = in.next();
    
    
                System.out.print("Street: " + in.nextLine());
                String street = in.nextLine();
    
                System.out.print("City: ");
                String city = in.next();
    
                System.out.print("State: ");
                String state = in.next();
    
                System.out.print("Telephone: ");
                String telephone = in.next();            
            }
       }
    }

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

    Default

    Yes, you need to first create an array of Customer, before the for loop, and fill it with objects from within the for loop.

  5. #5
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I'm at a loss now. I just read some things about that online not too long ago. I haven't covered anything like this in class...can't believe I'm being assigned this.
    Last edited by Kevinius; 04-03-2011 at 04:36 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Are you sure the assignment requires you to retain the data of all customers? If it does, learn about arrays here:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db

  7. #7
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Yes, it requires that I retain the information for printing later. We covered arrays a few weeks ago, but we didn't use them in this capacity. I guess it makes sense though...all of the information the user inputs is a string type...so the array must also be of the string type.

  8. #8
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    An example:

    Java Code:
    Scanner in = new Scanner(System.in);
    
    System.out.print("How many customers would you like to enter? ");
    
    int numberOfCustomers = in.nextInt();
    Customer[] customer = new Customer[numberOfCustomers];
    
    for(int i = 0; i <= numberOfCustomers; i++){
        customer[i] = new Customer(i);
    }
    Last edited by Vase; 04-03-2011 at 06:58 AM.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As vase pointed out, since you are using the user input to create customer objects you use an array of customers.

  10. #10
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    How am I supposed to retrieve the information the user has inputted? I guess more importantly, how the information stored in the array?? I would imagine that I'd need an array with 6 elements (to store each response). Am I correct in that assumption?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Not quite, you use a class to encapsulate all the data. So you prompt for input, create an object of that class from the input, then add that object to the array. You should always try to avoid parallel arrays if you can.

  12. #12
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    What information is the user inputting?

    If the information you enter is different for each object, you could create mutator and accessor methods and use them to store and retrieve information for each.
    Java Code:
    // an example
    customer[0].setInfo(userInputInfo);
    customer[4].getInfo();

  13. #13
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    This is what I have so far. I'm trying to understand how to get the information from the array. Six things are being inputted (first name, last name, street, city, state, and number). Where do I get access to this?

    Java Code:
    public class CustomerDemo {
    
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
    
    
            System.out.print("How many customers would you like to enter? ");
            while(!in.hasNextInt()){
                System.out.println("'" + in.nextLine() + "'" + " is not a valid entry. Please enter a number: ");
            }
            int numberToCreate = in.nextInt();
            Customer[] customer = new Customer[numberToCreate];
    
    
            for(int i = 0; i < numberToCreate; i++){
                customer[i] = new Customer(i);
    
                System.out.println("Please enter customer " + (i + 1) + " data:");
                System.out.print("First Name: ");
                String first = in.next();
    
                System.out.print("Last Name: ");
                String last = in.next();
    
    
                System.out.print("Street: " + in.nextLine());
                String street = in.nextLine();
    
                System.out.print("City: ");
                String city = in.nextLine();
    
                System.out.print("State: ");
                String state = in.next();
    
                System.out.print("Telephone: ");
                String telephone = in.next();
    
                System.out.println();
    
            }
        }
    }

  14. #14
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    If I wanted to create 2 customers, it appears that when the new Customer[] is initialized it will only have 2 elements...far less than what is needed.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I suggest you look up using mutators and accessors in the java tutorial. You could also develop a constructor which takes those 6 arguments, collect the arguments first then create the item with the 6 arguments used in the constructor. For more information on constructors check out the sun tutorials.

    Finally accessors will allow you to extract information from the objects in the array.

  16. #16
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Create mutator and accessor methods in your Customer class. I'll do an example for you.

    Java Code:
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    This is a mutator method. You access it by typing variableName.method(). In your case, where you currently have "String first = in.next;" you would instead have.
    Java Code:
    customer[i].setFirstName(in.next());
    Remember, customer[] is an array of Customer objects, and customer[i] is a single Customer in the array. When i = 2, you access the third customer in element customer[2], and use it's setFirstName method to set the firstName data field of that object. Write another method, getFirstName, that returns the firstName of the Customer object as a String.


    On second thought, sunde's way works as well, and is a bit more organized. However, mutator and accessor methods should always be created if you ever plan on changing or retrieving certain information from an object.
    Last edited by Vase; 04-03-2011 at 07:26 AM.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Create a Customer class that has a field for each element of information about a customer, and provide it with a constructor that initializes those fields.

    Each element of the array will be a Customer.

    db

  18. #18
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Alright...this is making more sense. What you all are talking about are getters and setters, right? I do know how to use these. I have created a Person class and a Customer class (derived from Person). I have created a constructor in Customer class that accesses the variables defined in Person class: I'll post the classes below:

    Java Code:
    public class Person {
        private String firstName;
        private String lastName;
        private String streetAddress;
        private String city;
        private String state;
        private String telephone;
    
        public Person(){
        }
    
        public Person(String firstName, String lastName, String streetAddress, String city, String state, String telephone) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.streetAddress = streetAddress;
            this.city = city;
            this.state = state;
            this.telephone = telephone;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getState() {
            return state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public String getStreetAddress() {
            return streetAddress;
        }
    
        public void setStreetAddress(String streetAddress) {
            this.streetAddress = streetAddress;
        }
    
        public String getTelephone() {
            return telephone;
        }
    
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
    
    }
    
    public class Customer extends Person {
        private int customerNumber;
        private boolean answer;
    
        public Customer(int customerNumber){
            this.customerNumber = customerNumber;
        }
        public Customer(String firstName, String lastName, String streetAddress, String city, String state, String telephone, int customerNumber, boolean answer){
            super(firstName, lastName, streetAddress, city, state, telephone);
    
            this.answer = answer;
        }
    
        public void removeFromList(){
            if(answer = true){
                System.out.println("Customer " + customerNumber + " is not on the mailing list.");
            }else{
                System.out.println("Customer " + customerNumber + " remains on the mailing list.");
            }
        }
    }
    
    public class CustomerDemo {
    
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
    
    
            System.out.print("How many customers would you like to enter? ");
            while(!in.hasNextInt()){
                System.out.println("'" + in.nextLine() + "'" + " is not a valid entry. Please enter a number: ");
            }
            int numberToCreate = in.nextInt();
            Customer[] customer = new Customer[numberToCreate];
    
    
            for(int i = 0; i < numberToCreate; i++){
                customer[i] = new Customer(i);
    
                System.out.println("Please enter customer " + (i + 1) + " data:");
                System.out.print("First Name: ");
                String first = in.next();
    
                System.out.print("Last Name: ");
                String last = in.next();
    
    
                System.out.print("Street: " + in.nextLine());
                String street = in.nextLine();
    
                System.out.print("City: ");
                String city = in.nextLine();
    
                System.out.print("State: ");
                String state = in.next();
    
                System.out.print("Telephone: ");
                String telephone = in.next();
    
                System.out.println();
    
            }
        }
    }
    I haven't implemented the changes that were mentioned regarding CustomerDemo class. Seeing how I'm creating objects of the class Customer, I must have the setters and getters there as well, right?
    Last edited by Kevinius; 04-03-2011 at 07:39 AM.

  19. #19
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Exactly. It looks like you've got it now. Now you just need to use the user input in your CustomerDemo class to store the data in your Customer object's data fields.

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    A for loop would work well for this.
    But if your company values a large LOC count, don't use a loop.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  2. Creating a Table with user input
    By JonniBravo in forum Eclipse
    Replies: 1
    Last Post: 09-08-2010, 12:50 PM
  3. geting user input (number)
    By kliane in forum New To Java
    Replies: 9
    Last Post: 01-17-2010, 12:25 PM
  4. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:00 PM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 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
  •