Java从另一个类调用方法 [英] java calling a method from another class

查看:83
本文介绍了Java从另一个类调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个问题,但由于刚开始学习Java,我感到非常困惑.我可以理解的任何帮助都会很棒.我必须编写一个具有两个类的程序.主类将从文件中读取内容,并使用第二类来查找文件中相同单词被重复的次数,并将它们添加到包含单词和单词重复次数的数组中.我可以阅读文件部分.我只是似乎不知道如何从第二个类中调用方法以将单词添加到数组中并增加计数器. 到目前为止,这是我的代码,如果您运行它,您将看到多少错误会告诉我我有多新.

I am working on a problem and I am very stuck because I am just starting to learn java. Any help I can get to understand this would be great. I have to write a program that has two classes. The main class will read from a file and uses the second class to find how may times the same words have been repeated in the file and add them to an array that contans the words and number of times the word repeated. I am ok with the reading the file part. I just can't seem to wrap my head around how to call a method from the second class to add the word into the array and increment the counter. Here is my code so far if you run it you will see how new I am to this by how many errors you will get.

import java.io.*;

public class Words{
public static void main (String [] args)
{
    ProcessInput();
    System.out.println("\nprogram finished");
}


public static WordList ProcessInput( )
{
    BufferedReader inputFile;
    String inputLine;
    String[] word;
    WordList words;
        try
        {
            inputFile=new BufferedReader(new FileReader ("inputFile.txt"));
            inputLine = inputFile.readLine();
            while (inputLine !=null)
            {
                word=inputLine.toLowerCase().split(" ");
                for (int i=0; i<word.length; i++){
                    System.out.println (word[i]);
                    words=addWord(word[i]);
                }
                inputLine = inputFile.readLine();

            }
            inputFile.close();
        }
        catch (IOException ioe)
        {
            System.out.println (ioe.getMessage());
            ioe.printStackTrace ();
        }
        return words;
}

}

class WordList {
String [] words;
int wordcount;
public WordList ( ){
    words= new String [1000];
    wordcount=0;

}

public String  addWord (String word) {
    words[wordcount]=word;
    wordcount=+1;
    return words[wordcount];

}

public void printList (){
    for (int i=0; i<wordcount; i++){
        System.out.println (words[i]);
    }
}
}

推荐答案

您非常亲密.您需要记住的是,当您从另一个类中调用一个方法时,需要告诉编译器在哪里可以找到该方法.

You're very close. What you need to remember is when you're calling a method from another class you need to tell the compiler where to find that method.

因此,除了简单地调用addWord("someWord")外,您还需要初始化WordList类的实例(例如WordList list = new WordList();),然后使用该实例调用方法(即list.addWord("someWord");.

So, instead of simply calling addWord("someWord"), you will need to initialise an instance of the WordList class (e.g. WordList list = new WordList();), and then call the method using that (i.e. list.addWord("someWord");.

但是,此刻您的代码仍然会在此处引发错误,因为这将试图从静态方法中调用非静态方法.因此,您可以将addWord()设为静态,或者更改Words类中的方法以使其不是静态的.

However, your code at the moment will still throw an error there, because that would be trying to call a non-static method from a static one. So, you could either make addWord() static, or change the methods in the Words class so that they're not static.

我对上面的段落不好-但是您可能想重新考虑ProcessInput()作为静态方法-确实需要吗?

My bad with the above paragraph - however you might want to reconsider ProcessInput() being a static method - does it really need to be?

这篇关于Java从另一个类调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆