Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

How do I control the annoying cursor on the console?

Thread view: 
John - 15 Apr 2007 18: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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.