Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java calling a method from another class

Tags:

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]);
    }
}
}
like image 763
blackStar Avatar asked Oct 15 '11 06:10

blackStar


People also ask

Can we call Main method from another class in Java?

Solution: Though Java doesn't prefer main() method called from somewhere else in the program, it does not prohibit one from doing it as well. So, in fact, we can call the main() method whenever and wherever we need to.

How do you call a method from another class without creating an object in Java?

We can call a static method by using the ClassName. methodName. The best example of the static method is the main() method. It is called without creating the object.


2 Answers

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?

like image 174
charlemagne Avatar answered Oct 22 '22 21:10

charlemagne


You have to initialise the object (create the object itself) in order to be able to call its methods otherwise you would get a NullPointerException.

WordList words = new WordList();
like image 44
aseychell Avatar answered Oct 22 '22 21:10

aseychell