Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

java calling a method from another class

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("
program 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]);
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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");.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...