Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 3rd, 2005, 01:39 PM
seanleevic seanleevic is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 1 seanleevic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 30 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old October 4th, 2005, 04:16 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 706 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 25 m
Reputation Power: 4
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:
Original - java Code
  1.  
  2.  
  3. import java.io.*;
  4.  
  5. class Employee {
  6.  
  7.   String name;
  8.   String telephone;
  9.   double income;
  10.  
  11.   public Employee (String n, String t, double i) {
  12.     name = n;
  13.     telephone = t;
  14.     income = i;
  15.   }
  16. }
  17.  
  18.  
  19. class ArrayOfObjects {
  20.  
  21.   public static void main(String[] args) {
  22.  
  23.     try
  24.     {
  25.       BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
  26.  
  27.       System.out.print( "Please enter number of employees: " );
  28.       int nrOfEmployees = Integer.parseInt(input.readLine());
  29.  
  30.       /* initialise array with given length */
  31.       Employee[] myEmployee = new Employee[nrOfEmployees];
  32.  
  33.       for( int i=0; i < myEmployee.length; i++ )
  34.       {
  35.         System.out.print( "Name of employee " +(i+1)+ ": " );
  36.         String name = input.readLine();
  37.  
  38.         System.out.print( "Telephone number of employee " +(i+1)+ ": " );
  39.         String tel = input.readLine();
  40.  
  41.         System.out.print( "Income of employee " +(i+1)+ ": " );
  42.         double income = Double.parseDouble(input.readLine());
  43.  
  44.         myEmployee[i] = new Employee( name, tel, income );
  45.       }
  46.  
  47.       for (int i=0; i < myEmployee.length; i++) {
  48.         System.out.println (myEmployee[i].name + ", " + myEmployee[i].telephone + ", " + myEmployee[i].income);
  49.       }
  50.     } catch( Exception e )
  51.     {
  52.       e.printStackTrace();
  53.     }
  54.   }
  55. }

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > User input and Array of Object


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT