英文:
The required type and my provided parameter are different
问题
以下是翻译好的内容:
我正在编写一个侦察系统的代码,我有多个类,层次结构如下:iScoutMember -> Scout(实现iScoutMember) -> BeaverScout(扩展Scout)。我还有一个ScoutList类,用于处理将侦察员添加到侦察员ArrayList中,以及ScoutSystem类,它创建一个菜单并使用ScoutList类中的方法。
我遇到的错误来自ScoutSystem类中的addScout方法,当我尝试为ArrayList SpecialInterests添加信息时出错。
这是错误信息:
**错误:(108, 103)java:不兼容的类型:无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**
**错误:(112, 103)java:不兼容的类型:无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**
这是Scout类的代码:
import java.util.ArrayList;
public abstract class Scout implements iScoutMember {
private String name;
private String county;
private String dateOfBirth;
private String address;
private String phoneNumber;
private ArrayList< SpecialInterest > specialInterests;
public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList< SpecialInterest > specialInterests) {
this.name = name;
this.county = county;
this.dateOfBirth = dateOfBirth;
this.address = address;
this.phoneNumber = phoneNumber;
this.specialInterests = specialInterests;
}
...
这是SpecialInterest类的代码:
public class SpecialInterest {
private String interestCategory;
private String details;
private String dateBadgeReceived;
public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
this.interestCategory = interestCategory;
this.details = details;
this.dateBadgeReceived = dateBadgeReceived;
}
...
最后,这是Scout系统类中的addScout方法的代码:
private void addScout(){
System.out.println("您想要添加哪种侦察员?");
System.out.println("1. Beaver侦察员");
System.out.println("2. Cub侦察员");
System.out.println("3. Scouter");
int option = input.nextInt();
switch(option){
case 1:
System.out.println("姓名 => ");
System.out.println("县城 => ");
System.out.println("出生日期 => ");
System.out.println("地址 => ");
System.out.println("联系电话 => ");
String name = input.nextLine();
String county = input.nextLine();
String dateOfBirth = input.nextLine();
String address = input.nextLine();
String phoneNumber = input.nextLine();
System.out.println("输入特别兴趣详情");
System.out.println("特别兴趣类别");
System.out.println("详情");
System.out.println("获得徽章的日期");
String interestCategory = input.nextLine();
String details = input.nextLine();
String dateBadgeReceived = input.nextLine();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
System.out.println("您是否想输入另一个特别兴趣:Y/y表示是,N/n表示否 => ");
String ans = input.nextLine();
ans.toUpperCase();
if (ans.equals("Y")){
System.out.println("父母电话号码?");
String parentPhone = input.nextLine();
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
} else if(ans.equals("N")){
String parentPhone = "";
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
}
}
}
}
这是给我错误的代码行:
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
如果您有任何进一步的问题,请随时提问。
英文:
I am currently writing code for a scout system I have multiple classes and the hierarchy goes as follows iScoutMember -> Scout(implements iScoutMember) -> BeaverScout
(extends Scout). I also have a ScoutList
class that handles adding scouts to the ArrayList
of scouts and ScoutSystem
which makes a menu and uses the methods from the ScoutList
class.
The error I get is from the addScout method in the ScoutSystem
class when I try to add info for the ArrayList SpecialInterests
This is the error info
Error:(108, 103) java: incompatible types: SpecialInterest cannot be converted to java.util.ArrayList<SpecialInterest>
Error:(112, 103) java: incompatible types: SpecialInterest cannot be converted to java.util.ArrayList<SpecialInterest>
This is the code for the Scout class
import java.util.ArrayList;
public abstract class Scout implements iScoutMember {
private String name;
private String county;
private String dateOfBirth;
private String address;
private String phoneNumber;
private ArrayList<SpecialInterest> specialInterests;
public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList<SpecialInterest> specialInterests) {
this.name = name;
this.county = county;
this.dateOfBirth = dateOfBirth;
this.address = address;
this.phoneNumber = phoneNumber;
this.specialInterests = specialInterests;
}
and this is the code for the SpecialInterest class
public class SpecialInterest {
private String interestCategory;
private String details;
private String dateBadgeReceived;
public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
this.interestCategory = interestCategory;
this.details = details;
this.dateBadgeReceived = dateBadgeReceived;
}
and finally this is the code for the addScout method in the Scout system class
private void addScout(){
System.out.println("What kind of scout would you like to add?.");
System.out.println("1. Beaver Scout");
System.out.println("2. Cub Scout");
System.out.println("3. Scouter");
int option = input.nextInt();
switch(option){
case 1:
System.out.println("Name => ");
System.out.println("County=> ");
System.out.println("Date of Birth => ");
System.out.println("Address => ");
System.out.println("Contact number => ");
String name = input.nextLine();
String county = input.nextLine();
String dateOfBirth = input.nextLine();
String address = input.nextLine();
String phoneNumber = input.nextLine();
System.out.println("Enter Special Interest Details");
System.out.println("Special Interest Category");
System.out.println("Details");
System.out.println("date Badge Received");
String interestCategory = input.nextLine();
String details = input.nextLine();
String dateBadgeReceived = input.nextLine();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
System.out.println("Do you wish to enter another special Interest: Y/y for yes, N/n for no ==> ");
String ans = input.nextLine();
ans.toUpperCase();
if (ans.equals("Y")){
System.out.println("Parents Phone Number?");
String parentPhone = input.nextLine();
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
} else if(ans.equals("N")){
String parentPhone = "";
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
}
}
}
}
This is the line of code that gives me the error
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
Any and all help will be much appreciated I am completely new to inheritance and to stack overflow so please be patient with me if my question is not formatted properly.
答案1
得分: 1
你的问题并不是非常清楚,但从我所理解的来看,你在addScout方法中传递了一个单独的SpecialInterest
对象(sp1
):
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
//其他操作
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
你可能应该创建一个新的ArrayList<SpecialInterest>
并将其传递进去,就像这样:
ArrayList<SpecialInterest> sps = new ArrayList<>();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
sps.add(sp1);
//其他操作
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sps, parentPhone);
英文:
Your question isn't extremely clear, but from what I can tell, you're passing in a single SpecialInterest
object (sp1
) in the addScout method at:
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
//stuff
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
What you probably should do is make a new ArrayList<SpecialInterest>
and pass that in, like this
ArrayList<SpecialInterest> sps = new ArrayList<>();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
sps.add(sp1);
//stuff
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sps, parentPhone);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论