链接到数组时没有找到合适的构造函数错误

时间:2021-09-29 18:54:40

the code is:

代码是:

Person aTrainee = new Trainee (firstName,lastName,streetAddress,postCode,phoneNumb,performanceAverage,trainingArea);

人aTrainee =新实习生(firstName,lastName,streetAddress,postCode,phoneNumb,performanceAverage,trainingArea);

and the error says

并且错误说

no suitable constructor found for Trainee...ectect

Person is an array

人是一个数组

Trainee is an item in the array (one of three, trainee, employee and management)

实习生是阵列中的一个项目(三个中的一个,实习生,员工和管理人员)

If I remove the items in the brackets then I do not get the error, but without it, the code does not work. I have made sure that the items in the brackets match up with the rest of the code 10+ times, I have spent over 1 hour trying to figure this out, I would love ANY advice you can give me.

如果我删除括号中的项目然后我没有得到错误,但没有它,代码不起作用。我确保括号中的项目与代码的其余部分匹配10次以上,我花了1个多小时试图解决这个问题,我很乐意为您提供任何建议。

edit: this is the code, there is A LOT of it so here is the important part.

编辑:这是代码,有很多它所以这里是重要的部分。

else if (userChoice.equalsIgnoreCase(toTrainee))
       {

           Scanner in = new Scanner(System.in);
           Scanner choice = new Scanner (System.in);
           System.out.println("To select a Trainee to edit, enter a value from 0 to 3: \n");
           int selection = choice.nextInt();

               if(selection <= 8)
               {
                   System.out.println("Incorrect, please enter a valid number!");
                   sc.nextLine();
                   break;
                }

            System.out.println("Please enter a first name for the Trainee \n");
            String firstName = in.nextLine();

            System.out.println("Please enter a Last name for the Trainee \n");
            String lastName = in.nextLine();

            System.out.println("Please enter a Street Address for the Trainee: \n");
            String streetAddress = in.nextLine();

            System.out.println("Please enter a Post code for the Trainee: \n");
            int postCode = in.nextInt();
            in.nextLine();

            System.out.println("Please enter a Phone Number for the Trianee: \n");
            String phoneNumb = in.nextLine();

            System.out.println("Please enter a performance average for the Trainee: \n");
            String performanceAverage = in.nextLine();

            System.out.println("Please enter a Training area for the Trainee: \n");
            String trainingArea = in.nextLine();

            Person aTrainee = new Trainee (firstName,lastName,streetAddress,postCode,phoneNumb,performanceAverage,trainingArea);
            System.out.println(aTrainee);
            myWorker.set(selection,aTrainee);

        }

Trainee constructor

public class Trainee extends Person
{
    private String performanceAverage, trainingArea;
    public Trainee()
    {
       super();
       performanceAverage = "";
       trainingArea = "";
    }
    public Trainee(String myFirstName, String myLastName, String myStreetAddress, int myPostCode, int myPhoneNumb, String myPerformanceAverage, String myTrainingArea)
    {
        super(myFirstName,myLastName,myStreetAddress,myPhoneNumb,myPostCode);
        performanceAverage = myPerformanceAverage;
        trainingArea = myTrainingArea;
    }

    public void setPerformanceAverage(String myPerformanceAverage)
    {
    this.performanceAverage = myPerformanceAverage;
    }

    public void setTrainingArea(String myTrainingArea)
    {
    this.trainingArea = myTrainingArea;
    }

     public String toString()
    {
        return super.toString() + ", Performance Average is " + performanceAverage + ", and Training Analysis is  " + trainingArea;
    }
}

This is Person which gives Trainee a few more things like Name ect.

这是Person,它为Trainee提供了更多名称等名称。

public class Person
{ 
       private String firstName;
       private String lastName;
       private String streetAddress;
       private int postCode;
       private int phoneNumb;

       public Person()
       {
           firstName = lastName = streetAddress = "";
           postCode = phoneNumb = 0;
       }

       public Person(String myFirstName, String myLastName, String myStreetAddress, int myPostCode, int myPhoneNumb)
       {
           firstName = myFirstName;
           lastName = myLastName;
           streetAddress = myStreetAddress;
           postCode = myPostCode;
           phoneNumb = myPhoneNumb;
        }

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

        public String getFirstName()
        {
            return firstName;
        }

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

        public String getLastName()
        {
            return lastName;
        }

        public void setStreetAdress(String myStreetAddress)
        {
        this.streetAddress = myStreetAddress;
        }

        public String getStreetAdress()
        {
        return streetAddress;
        }

        public void setPostCode(int myPostCode)
        {
        this.postCode = myPostCode;
        }

        public int getPostCode()
        {
        return postCode;
        }

        public void setPhoneNumb(int myPhoneNumb)
        {
        this.phoneNumb = myPhoneNumb;
        }

        public int getPhoneNumb()
        {
        return phoneNumb;
        }

        public String toString()
        {
             return "This person's information is: " + firstName + " " + lastName + ", " + " " + streetAddress + " " + postCode + ", phone number is  " + phoneNumb;
        }
}

and this is the array (note the first line is just a test so ignore the entries)

这是数组(注意第一行只是一个测试,所以忽略条目)

import java.util.ArrayList;
public class ShopMan
{
  public static void main()
  {
     ArrayList<Person>myWorker = new ArrayList<Person>();
     Person aShopEmployee = new ShopEmployee("Yazz","Hasan","1/43", 4215, 55271095, "Timber", 010101001, 50000);
     myWorker.add(aShopEmployee);
     aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);
      myWorker.add(aShopEmployee);
     aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);
      myWorker.add(aShopEmployee);
     aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);

     Person aManagement = new Management("","","",00,00,"",00,00,false);
     myWorker.add(aManagement);
     aManagement = new Management("","","",00,00,"",00,00,false);
     myWorker.add(aManagement);
     aManagement = new Management("","","",00,00,"",00,00,false);
     myWorker.add(aManagement);

     Person aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);
     aTrainee = new Trainee("", "", "", 00, 00, "", "");
     myWorker.add(aTrainee);

     for (int i=0; i < myWorker.size(); i++)
     {
        System.out.println(myWorker.get(i));
     }

     while (true)
     {
         Menu.main(myWorker);
     }
  }
}

ANSWER FOUND!

String phoneNumb = in.nextLine();

needed to be

需要

int phoneNumb = in.nextInt();
                in.nextLine();

Thank you so much to Russell Zahniser and creinig and everyone else

非常感谢Russell Zahniser和creinig以及其他所有人

1 个解决方案

#1


2  

Check the type of the phoneNumb parameter.

检查phoneNumb参数的类型。

#1


2  

Check the type of the phoneNumb parameter.

检查phoneNumb参数的类型。