我做错了什么 - 无法从主实例化对象(自定义类)

发布于 2024-11-04 21:21:56 字数 3012 浏览 1 评论 0原文

举起手来,我正在 OU 的 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,并将给自己整整两个月的时间,这是更明智的想法!

Hands up, I'm struggling with a programming question for M257 at OU, its formative and carries no marks and is due in a few days. I can't call the constructor from the test class and have struggled for several hours to no avail, the class compiles in Netbeans 6.91 fine but the constructor won't create the object. What am I doing wrong?

I had no problem with first question but am totally stuck here, obviously missing something significant - guidance please. The idea is to pass in the name of the file to the class, I can do the rest once I know the file is open and scanner initialised.

===============
/**
 * 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);
   }
}

When I try to comiple this is what it passes back.

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.

I haven't given up on this and will update question if I find the answer first.

8th May 2011: The answers were helpful but in the end although in the end I gave up on this question as the further I got I realised I just didn't know enough about how subclasses inherit from superclasses and need to try some simpler (and to me more meaningful) examples to deepen my understanding. The problem though was that NetBeans is too good at suggesting what you need without telling you exactly why its doing what it is doing, fine if your an experienced java developer, but not so good if your starting out.

I'm already started (i.e read the brief) for TMA02 and will give myself a full two months, much more sensible one thinks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-11-11 21:21:56

这不是构造函数。删除 boolean 作为返回类型 - 构造函数没有返回类型。所以:

public WordCounter(String fileToRead)

而不是

public boolean WordCounter(String fileToRead)

这就是错误告诉你的 - 编译器找不到具有该名称的构造函数。

请参阅:构造函数

This is not a constructor. Remove the boolean as return type - constructors don't have return types. So:

public WordCounter(String fileToRead)

instead of

public boolean WordCounter(String fileToRead)

And that's what the error tells you - the compiler cannot find a constructor with that name.

See: constructors

美人骨 2024-11-11 21:21:56

构造函数的签名是错误的。

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)

the signature of the constructor is wrong.

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();
        }
    }

use constructor like this. Replace the signature of constructor to

public WordCounter(String fileToRead)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文