Results 1 to 20 of 22
- 04-03-2011, 02:29 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
-
- 04-03-2011, 03:47 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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(); } } }
-
Yes, you need to first create an array of Customer, before the for loop, and fill it with objects from within the for loop.
- 04-03-2011, 04:32 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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.
- 04-03-2011, 05:22 AM #6
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
- 04-03-2011, 05:27 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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.
- 04-03-2011, 06:08 AM #8
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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.
- 04-03-2011, 06:32 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
As vase pointed out, since you are using the user input to create customer objects you use an array of customers.
- 04-03-2011, 06:53 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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?
- 04-03-2011, 07:00 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-03-2011, 07:00 AM #12
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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();
- 04-03-2011, 07:13 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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(); } } }
- 04-03-2011, 07:17 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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.
- 04-03-2011, 07:18 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-03-2011, 07:20 AM #16
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
Create mutator and accessor methods in your Customer class. I'll do an example for you.
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:public void setFirstName(String firstName) { this.firstName = firstName; }
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.Java Code:customer[i].setFirstName(in.next());
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.
- 04-03-2011, 07:20 AM #17
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
- 04-03-2011, 07:36 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 46
- Rep Power
- 0
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:
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?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(); } } }Last edited by Kevinius; 04-03-2011 at 07:39 AM.
- 04-03-2011, 07:42 AM #19
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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.
- 04-03-2011, 08:20 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM -
Creating a Table with user input
By JonniBravo in forum EclipseReplies: 1Last Post: 09-08-2010, 12:50 PM -
geting user input (number)
By kliane in forum New To JavaReplies: 9Last Post: 01-17-2010, 12:25 PM -
Creating objects based on a String value
By lvh in forum New To JavaReplies: 4Last Post: 04-30-2008, 02:00 PM -
Creating a dialog to input user/password
By prfalco in forum New To JavaReplies: 4Last Post: 02-18-2008, 07:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks