从main&调用方法(构造函数)文件格式

时间:2014-12-27 14:37:23

标签: java methods constructor

我有一个构造函数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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

时,会自动调用构造函数
ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);

答案 2 :(得分:0)

构造函数不是方法。方法的一个关键特征是它应该具有返回类型(如果它是&#39; void&#39;则为事件。)

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