有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从主文件和文件格式调用方法(构造函数)

我有一个构造函数ID3,我需要从main开始执行它。可能吗

我试着这样做:

public class ID3
{

    public static void main(String[] args) throws Exception 
    {
        System.out.print("\f"); //clears the screen
        ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);

        instance.ID3("data.txt", 3 , 5 , " ", 2); //error given here since this line had to be removed
    }

 public ID3(String fName, int numAttributes, int testCases, String delimiter, int limitSplits) throws IOException, FileNotFoundException
 {

    fileName = fName;
    n = numAttributes;
    t = testCases;
    numSplits = limitSplits;
    FileInputStream fstream = new FileInputStream(fileName);
    DataInputStream in = new DataInputStream(fstream);
    //Parse the first line to see if continuous or discrete attributes.
    firstLine = new String[n];
    firstLine = in.readLine().split(delimiter);
    int i, j, lineCount = 0;
    for(i=0; i<n; i++)
        unusedAttr.add(new Integer(i));
    input = new String[t][n+1];
    String line;
    int invalidLines = 0;
    while((lineCount + invalidLines)<t)
    {
        try
        {
            input[lineCount] = (in.readLine()).split(delimiter);
        }
        catch(NullPointerException e)
        {
            invalidLines++;continue;
        }
        if (Array.getLength(input[lineCount]) != n+1 || (Array.get(input[lineCount],n).toString().compareTo("?") == 0)) //number of attributes provided in the line is incorrect.
        {
            invalidLines++;continue;
        }
        lineCount++;
    }
    if(invalidLines == t)
    {
        System.out.println("All lines invalid - Check the supplied attribute number");
        System.exit(0);
    }
    if (invalidLines > 0)
        System.out.println("Not Considering "+invalidLines+" invalid training cases");
    if(numSplits > maxSplits || numSplits > (t/2))
    {
        System.out.println("numSplits should be less than or equal to "+Math.min(t/2,limitSplits));
        System.exit(1);
    }
    t = testCases - invalidLines;
    thresholdVal = new String[n][numSplits - 1];
    boolean allCont = false;
    if(Array.getLength(firstLine) == 1)
    {
        if(firstLine[0].compareTo("c") == 0)
            allCont = true;
        else if(firstLine[0].compareTo("d") == 0)
            return;
        else
        {
            System.out.println("Invalid first line - it should be c or d");
            System.exit(1);
        }
    }
    for(i=0; i<n; i++)
    {
        if(allCont || firstLine[i].compareTo("c") == 0) //Continuous Attribute
        {
            for(j=0; j<numSplits-1; j++)
                thresholdVal[i][j] = calculateThreshold(i,j);
        }
        else if(firstLine[i].compareTo("d") != 0)
        {
            System.out.println("Invalid first line - Training data (it should specify if the attributes are c or d)");
            System.exit(1);
        }
    }
    for(i=0; i<t; i++)
    {
        for(j=0; j<n; j++)
        {
            if(allCont || firstLine[j].compareTo("c") == 0)
                input[i][j] = makeContinuous(input[i][j], j);
        }
    }
}

上面显示了构造函数的代码,但是它找到了文件,但没有处理数据并打印出错误。文件应该是怎样的

使用的文本文件有:

d
Span Shape Slab
long square waffle
long rectangle waffle
short square two-way
short rectangle one-way

共 (3) 个答案

  1. # 1 楼答案

    不能像常规方法那样调用构造函数。当您创建一个类的实例时,即当您

    ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);
    
  2. # 2 楼答案

    构造函数不是方法。方法的一个关键特性是它应该有一个返回类型(如果是“void”,则为event)

    在这里,您不需要再次显式调用构造函数。在构造函数中实现的功能将在实例化时执行。但是,这是不推荐的,并且容易出现错误。您应该只实例化任何变量。实际功能应该在另一个方法中定义

  3. # 3 楼答案

    您已经在这里调用构造函数-ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);。不能将其作为常规方法调用。只需删除instance.ID3("data.txt", 5 , 14 , "", 5);