1

I am a beginner of Java. I am trying to create two Scanner objects from a class (which I have created by myself and there i also have created a constructor). I have initialized these two objects by the constructor call and later I want to update these two objects's information by obtaining data from user by Scanner class. When I am creating object by obtaining data from user by using Scanner object, I can create only one object and and the code for the second object are executing thoroughly without obtaining any data from the user. Thus I can create only one object and the code for second object is just displaying in the executing windows without obtaining any data.

Is it that I can't update the data of two objects by Scanner when they are already initialized by constructor call?

Here is the class code from where I want to create two object:

public class Employee {

//instance variables
String firstName;
String lastName;
double salary;

//constructor for three arguments
public Employee(String first_name, String last_name, double month_salary)
{
    firstName = first_name;
    lastName = last_name;
    if(month_salary > 0) //make sure that the salary is not smaller than zero

    {
        salary = month_salary;
    }
}//end of constructor

//set the value of the instance variables
        //set first name        
        public void setFirstName(String first_name)
        {
            firstName = first_name;
        }
        //get first name
        public String getFirstName()
        {
            return firstName;
        }

        //set last name
        public void setLastName(String last_name)
        {
            lastName = last_name;
        }

        //get last name
        public String getLastName()
        {
            return lastName;
        }

        //set salary
        public void setSalary(double month_salary)
        {
            if(month_salary > 0) //make sure that the salary is not smaller than zero

            {
                salary = month_salary;
            }
            else salary = 0.00;
        }

        //get salary
        public double getSalary()
        {
            return salary;
        }
   }

And here are the object's code

import java.util.Scanner;
public class EmployeeTest {

public static void main(String[] args) {

    Employee employee1 = new Employee("M", "R", 5000);
    Employee employee2 = new Employee("T", "M", 6000);

    System.out.println("Display the intial Employee's information\n");

    //showing the Employee2's information
    System.out.println("Displaying Employee1's information");

    System.out.printf("First Name: %s\nLast Name: %s\nSalary: %.2f\n", 
            employee1.getFirstName(), employee1.getLastName(), employee1.getSalary());

    System.out.println();

    //showing the Employee2's information
    System.out.println("Displaying Employee2's information");

    System.out.printf("First Name: %s\nLast Name: %s\nSalary: %.2f\n", 
            employee2.getFirstName(), employee2.getLastName(), employee2.getSalary());

    System.out.println();

    //create an scanner object to get information from user
    Scanner input = new Scanner(System.in);

    //obtain employee's first name from user
    System.out.print("Please enter the first name of employee1: ");
    String fName1 = input.nextLine();

    //set the first of employee1
    employee1.setFirstName(fName1);

    //obtain employee's last name from user
    System.out.print("Please enter the last name of employee1: ");
    String lName1 = input.nextLine();

    //set the last of employee1
    employee1.setLastName(lName1);

    //set the salary employee1's
    System.out.print("Please enter employee's salary: ");
    double empl_salary1 = input.nextDouble();

    //obtain the employe1's salary
    employee1.setSalary(empl_salary1);

    //display the updated employee1's information



      System.out.println("Updated information of employee's information");

    System.out.printf("First name :%s\nLast name: %s\nSalary: %.2f\n",
            employee1.getFirstName(),employee1.getLastName(), employee1.getSalary());

    //obtain employee2's first name from user   
    System.out.print("Please enter employee2'first name: ");
    String fName2 = input.nextLine();

    //set the first name of employee2
    employee2.setFirstName(fName2);


    //obtain employee's last name from user
    System.out.print("Please enter the employee2's last name: ");
    String lName2 = input.nextLine();

    //set the last name of employee2
    employee2.setLastName(lName2);

    //obtain the salary employee2's
    System.out.print("Please enter employee2's salary: ");
    double empl_salary2 = input.nextDouble();

    //set the employe1's salary
    employee2.setSalary(empl_salary2);


}

}

I don't know where is the problem?

Thanks for your time and assistance!

Mamun
  • 375
  • 2
  • 8
  • 17
  • I only see on `Scanner` in your code at the line `Scanner input = new Scanner(System.in);`. Where and why do you try to use a second one? –  Jul 23 '14 at 18:15
  • Thank your for your reply. I beg your pardon. I have made a mistake to ask the question. I am correcting it. – Mamun Jul 23 '14 at 18:16
  • See this post: http://stackoverflow.com/questions/11551985/java-nextline-and-nextdouble-differences Maybe use `Double.parseDouble(input.nextLine())` instead of `input.nextDouble()` – blueygh2 Jul 23 '14 at 18:28

1 Answers1

0

Add input.nextLine(); just before obtaining data for second employee:

input.nextLine();

//obtain employee2's first name from user   
System.out.print("Please enter employee2'first name: ");
String fName2 = input.nextLine();

It is because input.nextDouble(); "consumes" only double value, leaving the newline character you entered "unconsumed".

Kao
  • 7,225
  • 9
  • 41
  • 65