How do I control the annoying cursor on the console?

J

John

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
 
L

Lew

John said:
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?

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().
 
T

Tom Hawtin

John said:
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
 
J

John

Lew said:
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().

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=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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top