Search code examples
javaclasscastexception

How Can I Input A Custom Class Value Via Scanner?


I am trying to put together a small test application that takes inputs via scanner and puts them in-memory via hashmap and tree set.

Later on I'll search, edit, and delete them (So basically a CRUD), it requires 2 classes an employee class and a company one. I'm trying to take the input for the employee with all the information for the employee including the company which is a foreign custom class object in the POJO.

It won't let me class cast it, what should I do?

Here is the POJO

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    private String ssn;
    private Integer salary;
    private String birthDate;
    private String jobTitle;
    @ManyToOne
    private Company companyName;

   public Employee (String fN, String lN, String SSN, Integer sal, String birth, String jobT, Company compName) {       
    lastName = lN;
    firstName = fN;
    SSN = ssn;
    sal = salary;
    birth = birthDate;
    jobT = jobTitle;
    compName = companyName;
}

public String toString()
{
    return "Employee[Last Name= " + lastName + ", First Name= " + firstName + " SSN= " + ssn + ","
            + "Salary= " + salary + ", Birth Date= " + birthDate + ", Job Title= " + jobTitle + ",]" ;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getSsn() {
    return ssn;
}

public void setSsn(String ssn) {
    this.ssn = ssn;
}

public Integer getSalary() {
    return salary;
}

public void setSalary(Integer salary) {
    this.salary = salary;
}

public String getBirthDate() {
    return birthDate;
}

public void setBirthDate(String birthDate) {
    this.birthDate = birthDate;
}

public String getJobTitle() {
    return jobTitle;
}

public void setJobTitle(String jobTitle) {
    this.jobTitle = jobTitle;
}

public Company getCompanyName() {
      return companyName;
 }

public void setCompanyName(Company companyName) {
       this.companyName = companyName;
}
}

Here is part of the class the scanner is in

        System.out.println("Enter Company Name : ");
          String val7 = input1.nextLine();
          ...             
          Employee newEmp = new Employee(str1,str2, str3,    val4, str5, str6, val7);

If I pass the str7 in, it obviously creates an error as the method takes the CompanyName.

Any idea what I need to be doing as ClassCast doesn't work here.

EDIT here is the company class public class Company { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; String companyName; private String description;

public String getCompanyName() {
    return companyName;
}
public void setCompanyName(String companyName) {
    this.companyName = companyName;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
}

Solution

  • you need to create instance of Company, don't pass it as string:

    System.out.println("Enter Company Name : ");
    String val7 = input1.nextLine();
    Company company = new Company();
    company.setCompanyName(val7);
    

    and use company instead of val7 when you create Employee

    Employee newEmp = new Employee(str1,str2, str3,    val4, str5, str6, company);
    

    ALSO you have a logical error in the constructor, this

    compName = companyName; 
    

    should be the other way around ... like this

    companyName = compName; 
    

    because you want to assign the value from param to the member,