Home » Java » class – Java Program: Type mismatch: cannot convert from String to int Java-Exceptionshub

class – Java Program: Type mismatch: cannot convert from String to int Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was supposed to modify a program in Java by adding a constructor that accepts the following values as arguments and assigns them to the appropriate fields: employee’s name, employee’s ID number, department, and position. A constructor that accepts the following values as arguments and assigns them to the appropriate fields: employee’s name and ID number. The department and position fields should be assigned an empty string (“”). A no-arg constructor that assigns empty strings (“”) to the name, department, and
position fields, and 0 to the idNumber field. When I ran my code, I was given the message “Type mismatch: cannot convert from String to int.” Could someone tell me what I did wrong and how I should fix it?

Employee Class File:

public class Employee
{
    private String name;
    private int idNumber;
    private String department;
    private String position;

    public Employee(String name, int idNumber, String department, String position)
    {
        this.name = name;
        this.idNumber = idNumber;
        this.department = department;
        this.position = position;
    }
    public Employee(String name, int idNumber)
    {
        this.name = name;
        this.idNumber = idNumber;
        this.department = "";
        this.position = "";
    }
    public Employee()
    {
        this.name = "";
        this.idNumber = "";
        this.department = "";
        this.position = "";
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getIdNumber()
    {
        return idNumber;
    }
    public void setIdNumber(int idNumber)
    {
        this.idNumber = idNumber;
    }
    public String getDepartment() 
    {
        return department;
    }
    public void setDepartment(String department) 
    {
        this.department = department;
    }
    public int getPosition()
    {
        return position;
    }
    public void setPosition(String position)
    {
        this.position = position;
    }
    public String toString()
    {
        return getName() + "\t" + getIdNumber() + "\t" + getDepartment() + "\t" + getPosition();
    }
}

EmployeeDemo Class File:

public class EmployeeDemo
{
    public static void main(String[] args)
    {
        Employee emp1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
        Employee emp2 = new Employee("Mark Jones", 39110);

        emp2.setPosition("IT Programmer");
        emp2.setDepartment("Computer Science");

        Employee emp3 = new Employee();

        emp3.setName("Joy Rogers");
        emp3.setIdNumber(81774);
        emp3.setDepartment("Mechanical");
        emp3.setPosition("Engineer");

        System.out.println("Name\tID Number\tDepartment\tPosition");
        System.out.println(emp1.toString());
        System.out.println(emp2.toString());
        System.out.println(emp3.toString());
    }
}
How to&Answers: