Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am an eighth grader in need of some help with a small project I am doing in Java. I am trying to create an application that finds the solution of two linear equations (user enters two slopes and two y-intercepts (one for each equation)). The values that are returned from the "plotter" methods are stored into arrays. I have different methods for each of the "plotter" sections. I then need to be able to access both arrays in another method. But I get a "variable not found" error for both arrays when I try to "call" the method. The "code" below is just an outline of what I am working on (a rough one, at that). Please let me know if I need to include more. Thank You!

public class fooBar{
    public static void main(String[] args){
        fooBar.doStuff1();
        fooBar.doStuff2();

        fooBar.doMoreStuff(array1, array2);
    }

    public static void doStuff1(){
        //this method defines array1 (with long values) and assigns values to it
    }

    public static void doStuff2(){
        //this method defines array2 (with long values) and assigns values to it
    }

    public static void doMoreStuff (long[] array1, long[] array2){
        //this method needs to have access to array1 and array2
        //to do the calculations and such
    }
}
share|improve this question
    
I cringe whenever I see some []. We have collections, you know. Use them. –  Martin Schröder Mar 31 '12 at 18:39
    
@MartinSchröder What is a collection? o_O –  fr00ty_l00ps Apr 2 '12 at 13:53
    
See here. –  Martin Schröder Apr 2 '12 at 18:17
add comment

1 Answer

up vote 11 down vote accepted

How about simply returning the array?

public class fooBar{
    public static void main(String[] args){
        long[] array1 = fooBar.doStuff1();
        long[] array2 = fooBar.doStuff2();

        fooBar.doMoreStuff(array1, array2);
    }

    public static long[] doStuff1(){
        long[] array = new long[1234];
        // do stuff
        return array;
    }

    public static long[] doStuff2(){
        long[] array = new long[4321];
        // do stuff
        return array;
    }

    public static void doMoreStuff (long[] array1, long[] array2){
        //this method needs to have access to array1 and array2
        //to do the calculations and such
    }
}
share|improve this answer
    
I feel dumb for not thinking of that... X/ Thank you though, when I can, I will accept it. –  fr00ty_l00ps Mar 30 '12 at 17:10
    
Yep, it works with the real code. Again, thank you! –  fr00ty_l00ps Mar 30 '12 at 17:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.