如何添加没有构造函数参数的类实例?



我正在尝试创建一个梦幻足球选秀程序。我遇到困难的第一步是正确地将数据读取到列表中。我想逐行扫描数据。我创建了一个循环,设置每个角色的名称,然后添加所有拥有该角色的人,直到"-----"出现在一条线上。然后将该角色添加到"角色"列表中。我相信我的循环是正确的,但是,我不知道如何正确地使用我的Person构造函数,使其包含一个秩、名称和原点,这是我在Person类中使用setData方法无法实现的。我还是一个编程初学者,我想知道我是否遗漏了什么。

数据文件

领导者

1超人DC

2美国队长漫威

3教授X Marvel

4修维勒神秘人

布朗

1绿巨人漫威

2金刚狼漫威

3神奇

4野兽漫威

5雷神漫威

6神秘先生

7 Mr.Incredible Pixar

等等

主要类别

import java.util.HashMap;
import java.util.List;
import java.util.Scanner; 
import java.io.*; 
import java.util.ArrayList;
public class FantasyTeamDraft {
/**
 * Joseph Simmons
 * CPS 181
 * February 6, 2013
 */
public static void main(String[] args) throws IOException{
    Scanner scan = new Scanner (System.in);
    
    System.out.println("Enter the name and location of the file wanted for the draft: "); 
    File draftData = new File (scan.next());
    Scanner scanData = new Scanner(draftData); 
    
    List <Role> listOfRoles = new ArrayList <Role> (); 
    while (scanData.hasNext()) {
        String line = scanData.nextLine(); 
        if (!isInteger (line)) {
            Role role = new Role (); 
            role.setRoleName(line); 
            String personLine = scanData.nextLine(); 
            while (isInteger(personLine)){
                Person person = new Person(); 
                person.setData(personLine);
                role.addPerson(person); 
            }
            listOfRoles.add(role); 
    } 
        }
     
    
    
    
    
}
public static boolean isInteger (String line) {
    try {
        Integer.parseInt(line.split("t") [0]); 
    } catch (NumberFormatException e) {
        return false; 
    }
    return true; 
}

}

人员类别

import java.util.Scanner;
public class Person {
private int rank; 
private String name; 
private String origin;
public Person () {
    
}
public Person (int rank, String name, String origin) {
    this.rank = rank; 
    this.name = name; 
    this.origin = origin; 
}
public void setData (String line) {
    String [] array = line.split("t"); 
    this.rank = Integer.parseInt(array [0]); 
    this.name = array [1]; 
    this.origin = array [2]; 
}

}

角色类

    import java.util.List;
import java.util.Scanner;
public class Role {
private String roleName = ""; 
private List <Person> listOfPeople;
public Role (String roleName) {
    this.roleName = roleName;  
}
public void setRoleName (String line) {
    this.roleName = line; 
}
public String getRoleName() {
    return roleName; 
}
public void addPerson (Person person) {
    this.listOfPeople.add(person);  
}
    
    

}

在您的代码中,我看到

 while (isInteger(personLine)){
            Person person = new Person(); 
            person.setData(personLine);
            role.addPerson(person); 
        }

如果isInteger(personLine)的计算结果为true,这将是一个无限循环。也许应该是if而不是while,或者您需要在每次迭代中修改personLine

您有两个构造函数

公众人物(){

public Person (int rank, String name, String origin) {
this.rank = rank; 
this.name = name; 
this.origin = origin; 
}

如果我正确理解你的问题,为了设置等级、名称和出身的值,你会想调用第二个构造函数。但在你的循环中

while (isInteger(personLine)){
            Person person = new Person(); 
            person.setData(personLine);
            role.addPerson(person); 
        }

您在这里调用new Person(),请尝试将其更改为调用其他构造函数。

创建一个接受字符串的构造函数,就像setData一样,然后在该构造函数中调用setData方法,并将字符串发送给它。通过这种方式,您可以保留setData,而您的构造函数基本上是setData()。

如果你想完全保持构造函数的原样,你必须在主中拆分字符串

相关内容

最新更新