Java 我做错了什么-can';t从Main实例化对象(自定义类)

Java 我做错了什么-can';t从Main实例化对象(自定义类),java,inheritance,constructor,Java,Inheritance,Constructor,举手,我正在为麻省理工大学M257的一个编程问题而苦苦挣扎,这个问题是形成性的,没有分数,几天后就要到期了。我无法从测试类调用构造函数,并且已经挣扎了几个小时没有结果,该类在Netbeans 6.91中编译得很好,但构造函数不会创建对象。我做错了什么 我对第一个问题没有异议,但我完全被困在这里,显然遗漏了一些重要的东西——请指导。这个想法是将文件名传递给类,我可以在知道文件已打开且扫描仪已初始化后执行其余操作 =============== /** * Title: WordCounter c

举手,我正在为麻省理工大学M257的一个编程问题而苦苦挣扎,这个问题是形成性的,没有分数,几天后就要到期了。我无法从测试类调用构造函数,并且已经挣扎了几个小时没有结果,该类在Netbeans 6.91中编译得很好,但构造函数不会创建对象。我做错了什么

我对第一个问题没有异议,但我完全被困在这里,显然遗漏了一些重要的东西——请指导。这个想法是将文件名传递给类,我可以在知道文件已打开且扫描仪已初始化后执行其余操作

===============
/**
 * Title: WordCounter class
 * Description: M257 TMA01, Q2 - word counter class as described in instructions
 * @author Andrew Broxholme
 */

package tma01q2;

import java.io.*;
import java.util.*;


public class WordCounter
{  
    //Class instance variables
    public static int totalWords;
    public static int totalEven;
    public static int totalOdd;
    public static int totalLetters;

    private Scanner fileScanner;
    String sourceFile;
    String line;    //The lines of the text file

    //Single argument constructor, accepts source filename
    public boolean WordCounter(String fileToRead)
    {
        sourceFile = fileToRead;
        try
        {
            openRead();
            while (fileScanner.hasNext())
            {
                // Process each line of the text file
                line = fileScanner.nextLine();
                System.out.println(line);
       //         countWords();
            }
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
        finally
        {
            fileScanner.close();
        }
    }

    //openRead, opens the file and processes each line of the file until finished
    private boolean openRead() throws IOException
    {
        try
        {
            fileScanner = new Scanner(sourceFile);
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
    } 
    // More methods to be added   
}

/*
 * TestWordCounter.
 * Description: Tests the WordCounter class as per TMA01q2 instructions
 * @author Andrew Broxholme
 * V1.0 30th April 2011
 */

package tma01q2;

public class TestWordCounter
{
   //Create a WordCounter to process the specified text file.
   public static void main(String[] args)
   {
      String testFile = "haiku.txt";
      WordCounter fileStats = new WordCounter(testFile);
   }
}
当我试着把它混合起来时,这就是它传递回来的东西

Compiling 1 source file to C:\M257\TMA01\TMA01Q2\build\classes
C:\M257\TMA01\TMA01Q2\src\tma01q2\TestWordCounter.java:18: cannot find symbol
symbol  : constructor WordCounter(java.lang.String)
location: class tma01q2.WordCounter
      WordCounter fileStats = new WordCounter(testFile);
1 error
C:\M257\TMA01\TMA01Q2\nbproject\build-impl.xml:246: The following error occurred while executing this line:
C:\M257\TMA01\TMA01Q2\nbproject\build-impl.xml:113: Compile failed; see the compiler error output for details.
我并没有放弃这个问题,如果我先找到答案,我会更新这个问题


2011年5月8日:答案很有帮助,但最终,尽管最后我放弃了这个问题,因为我意识到我对子类如何继承超类知之甚少,需要尝试一些更简单(对我来说更有意义)的例子来加深我的理解。但问题是,NetBeans太擅长建议您需要什么,而不告诉您它为什么要做它正在做的事情,如果您是一名经验丰富的java开发人员,这很好,但如果您刚开始工作,就不太好了


我已经开始为TMA02做准备(即阅读简报),并将给自己整整两个月的时间,这比人们想象的要明智得多

这不是一个构造函数。删除作为返回类型的
布尔值
-构造函数没有返回类型。因此:

public WordCounter(String fileToRead)
而不是

public boolean WordCounter(String fileToRead)
这就是错误告诉您的-编译器找不到具有该名称的构造函数


请参阅:

这不是构造函数。删除作为返回类型的
布尔值
-构造函数没有返回类型。因此:

public WordCounter(String fileToRead)
而不是

public boolean WordCounter(String fileToRead)
这就是错误告诉您的-编译器找不到具有该名称的构造函数


请参阅:

构造函数的签名错误

public WordCounter(String fileToRead)
{
     sourceFile = fileToRead;
     try
      {
            openRead();
            while (fileScanner.hasNext())
            {
                // Process each line of the text file
                line = fileScanner.nextLine();
                System.out.println(line);
       //         countWords();
            }
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
        finally
        {
            fileScanner.close();
        }
    }
像这样使用构造函数。将构造函数的签名替换为

public WordCounter(String fileToRead)

构造函数的签名是错误的

public WordCounter(String fileToRead)
{
     sourceFile = fileToRead;
     try
      {
            openRead();
            while (fileScanner.hasNext())
            {
                // Process each line of the text file
                line = fileScanner.nextLine();
                System.out.println(line);
       //         countWords();
            }
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
        finally
        {
            fileScanner.close();
        }
    }
像这样使用构造函数。将构造函数的签名替换为

public WordCounter(String fileToRead)

好的,谢谢,这很有帮助。说明要求我返回true/false如果文件打开,那么处理完成后返回true/false,我将使用实例变量返回。好的,谢谢,这很有帮助。指令要求我返回true/false,如果文件打开,则在处理完成后返回true/false,我将使用实例变量执行此操作。“但问题是,NetBeans太善于提出你需要的东西,而不告诉你它为什么要这样做,如果你是一名经验丰富的java开发人员,这很好,但如果你刚开始工作,就不太好了。”+1但问题是,NetBeans太善于提出你需要的东西,而不告诉你它为什么要这样做,如果你是一个有经验的java开发人员,这很好,但是如果你刚开始工作,就不太好了。”+1