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
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 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: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
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
  #3  
Old January 7th, 2009, 09:49 AM
maryam maryam is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 1 maryam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 25 sec
Reputation Power: 0
problem

Quote:
Originally Posted by Icon
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. }

I am not getting this... why you use exception handling here..kindly please tell me

Reply With Quote
  #4  
Old April 13th, 2009, 05:41 AM
RamyaSivakanth RamyaSivakanth is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 1 RamyaSivakanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 45 sec
Reputation Power: 0
Userinput and arfrfay of object

Hi Sean,
One small suggestion in the code as you asked.Instead of using Bufferedreader ,you can use Scanner class which is in util package.In this class ,you can read the integer,double and string data from the console without typecasting
Example :
Scanner scannerObject = new Scanner(System.in);
//Reads the input as integer
int noOfEmployees = scannerObject.nextInt();
//Reads the input as String
String name = scannerObject.nextLine();
//Reads the input as string
String tel = scannerObject.nextLine()
//Reads the input as double
double income = scannerObject.nextDouble();

Reply With Quote
Reply

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


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap