I have an employee class that needs name and age to be input from the
console, but I cannot control the cursor, which always go to the front of
the prompt line, making my program look not so professional.
Is there anyway that I can let it go to the end of the prompt?
public class employee
{
public employee() {
Scanner input = new Scanner( System.in );
System.out.println("Your name: ");
name = input.nextLine();
System.out.println("Your age: ");
age = input.nextLine();
}
..........................
private String name;
private String age;
}
Besides, I feel the constructor is ugly because I wonder whether there is
anyway to insert the prompt in the argmument parentheses? Any suggestion?
Thanks!
John
Lew - 15 Apr 2007 19:00 GMT
> I have an employee class that needs name and age to be input from the
> console, but I cannot control the cursor, which always go to the front of
[quoted text clipped - 19 lines]
> Besides, I feel the constructor is ugly because I wonder whether there is
> anyway to insert the prompt in the argmument parentheses? Any suggestion?
Yes. Two.
Name the class with an initial upper-case letter, and each word part similarly
should have an upper-case initial letter, viz., "Employee".
Do not do work in the constructor. Constructors are to build the object, not
execute its logic. Execute the logic from an instance method attached to an
instance created in another class's method or in this class's main().

Signature
Lew
John - 15 Apr 2007 21:05 GMT
>> I have an employee class that needs name and age to be input from the
>> console, but I cannot control the cursor, which always go to the front of
[quoted text clipped - 29 lines]
> to an instance created in another class's method or in this class's
> main().
Are you suggesting this:
public class Employee
{
public employee(String name, int age) {
this.name=name;
this.age = age;
}
..........................
private String name;
private String age;
}
public class Team
{
public Team(int numberofemployee)
{
Employee[] group=new Employee[numberofemployee];
}
public void addEmployee()
{ for (i=0; i<numberofemployee;i++)
Scanner input = new Scanner( System.in );
System.out.println("Your name: ");
name = input.nextLine();
System.out.println("Your age: ");
age = input.nextLine();
group[i]=new Employee(name, age)
}
.....................................
private Employee[] group;
private final integer numberofemployee=10
}
This is better, isn't it?
All the keyboard input and screen output are done in another class(Team)'s
instance method (addEmployee).
Is this what you suggested?
Tom Hawtin - 15 Apr 2007 19:21 GMT
> I have an employee class that needs name and age to be input from the
> console, but I cannot control the cursor, which always go to the front of
> the prompt line, making my program look not so professional.
> Scanner input = new Scanner( System.in );
> System.out.println("Your name: ");
> name = input.nextLine();
Use PrintStream.print instead of PrintStream.println. println appends a
*n*ew *l*ine sequence to the end of the output, hence the *ln*.
Tom Hawtin