0

I'm having difficulty processing an array of numbers from a class file (Scores.java) within a separate class file (ProcessScores.java).

Adding Scores.main(); within the main void of ProcessScores.java does print the array but my attempts to work with the output have failed. For instance, int[] numbers = Scores.main(); throws the error "Incompatible Types. Required: int[], Found: void". I understand that I'm calling the main void from the Scores class so my question is...

How can I get the output from the Scores class into the ProcessScores class in a way that I can work with it?

2
  • well, the main() method is always void and since it's static, you can't use it to return the results. You could try writing other static method that returns something instead Commented Apr 3, 2014 at 0:03
  • Can you show the structure of your classes? I would suggest writing a function like int[] getNumbers() for Scores, that would return the array of numbers stored in the Scores class. Then you can do int[] numbers = Scores.getNumbers() inside of your ProcessScores main method. Commented Apr 3, 2014 at 0:05

2 Answers 2

1

You have 1 project with 2 classes which each contain a main? A project should only have 1 main class. Try something like this:

public class A {
    public static void main(String[] args) {
        B b = new B();
        int[] array = b.getArray();
        // Do something with array
        b.setArray(array);
    }
}

public class B {
    private int[] array;
    public B(){
        array = {0,1,2,3,4}; // Dummy data
    }

    public int[] getArray(){
        return array;
    }

    public void setArray(int[] array){
        this.array = array;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

the main() method is void (does not return anything) and since it's static, you can't use it to return the results. You could try writing other static method that returns something instead.

Try creating another public static method that returns int[] and use it instead.

For example

public static int[] mymethod(){
  ...
}

And call it like

int[] result = Scores.mymethod();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.