|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
User input and Array of Object
Good day. I have a question on user input and array of object.
Please look at the code below and follow with my questions: class Employee { String name; String telephone; double income; public Employee (String n, String t, double i) { name = n; telephone = t; income = i; } } class ArrayOfObjects { public static void main(String[] args) { Employee[] myEmployee = new Employee[3]; myEmployee[0] = new Employee("Anne", "138-111", 230.25); myEmployee[1] = new Employee("Billy", "238-222", 500.58); myEmployee[2] = new Employee("Catty", "338-333", 800.88); for (int i=0; i < myEmployee.length; i++) { System.out.println (myEmployee[i].name + ", " + myEmployee[i].telephone + ", " + myEmployee[i].income); } } } My question: 1. I need to accept user input (ask user how many employees he/she wants to enter). My teacher asked us to use keyboard.readint() to read user input. 2. After the user input (ie: 3), I have to put the input into array of object. Although the code above uses array of object, but how can i accept the user input and put it into the array. My problem is that I have no idea where to insert the " System.out.println("Please enter number employees: ); NoEmp = Keyboard.readint(); and how and where should I put the NoEmp to declare the number of employee the user key in? I am totally new to Java programming and hope I can start somewhere with kind people helping me to understand Java. It is good if anyone can direct me to any good java learning sites. Thank you very much. Regards, Sean |
|
#2
|
|||||
|
|||||
|
Maybe this example will help you. I did not use readInt() so if your teacher really wants that you should look into it. BufferedReader does not have a readInt() and DataInputStream which does have a readInt() has a deprecated readLine()...
Now this program is BAD . It does no checking of user input or anything. One big try - catch block is bad coding, I just did this to show you where to put the 'Enter number of employees' and how to construct the array from user input. In a good program you should check all exceptions seperately and take actions accordingly, i.e., when a user is queried for his income and he enters 'not enough' then you should respond with 'That's too bad, just enter a number dude! (negative numbers are also allowed)' Hope this helps you java Code:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > User input and Array of Object |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|