-2

I'm having difficulty passing arrays between methods, I've managed to set them all to false from boolean, and returned the array to the main. However from there I don't know how to pass it to another method, and then later display the boolean true array as "yes" or the boolean false array as "no". My code looks as follows:

import javax.swing.*;
class methodarrays
{
    public static void main (String[]param)
    {
        arrays();
        seen(); 
        display();
    }

    public static boolean[] arrays()
    {
        boolean [] birds = new boolean [5];
        for (int i=0;i<birds.length;i++)
        {
            birds[i]=false;
        }
        return birds;
    }
    public static boolean seen()
    {
        String quit = "100";
        String ans = "";
        while(!ans.eqauls(quit))
        {
            ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit   2) Blackbird   3)Robin   4)Wren   5)Greenfinch");
            if (ans.equals("1"))
            {
                birds[0] = true;
                return birds[0];
            }
            else if (ans.equals("2"))
            {   birds[1] = true;
                return birds[1];
            }
            else if (ans.equals("3"))
            {
                birds[2] = true;
                return birds[2];
            }
            else if (ans.equals("3"))
            {
                birds[2] = true;
                return birds[2];
            }
            else if (ans.equals("4"))
            {
                birds[3] = true;
                return birds[3];
            }
            else if (ans.equals("5"))
            {
                birds[4] = true;
                return birds[4];
            }
        }
    }

    public static void display()
    {
        JOptionPane.showMessageDialog(null,"Your Garden Watch results are:");
    }

}
resueman
  • 10,572
  • 10
  • 31
  • 45
duldi
  • 180
  • 1
  • 17
  • 2
    You need to learn basic OO programming before asking such questions and read guides on "How to learn Java" – Adel Boutros Nov 17 '15 at 15:27
  • Based on your implementation, whenever you call arrays() method you will automatically re-create birds array and re-set its content to False. I think at this stage it's difficult to understand what is your objective. – e.doroskevic Nov 17 '15 at 15:28
  • @E.Doroskevic I'm attempting to change the boolean values, if a certain characters met. I want the boolean array to be one method and have it passed to the next methods – duldi Nov 17 '15 at 15:30
  • 1
    @duldi I appreciate that, however your implementation does not reflect that. See, based on what you have coded-up, whenever you call method arrays() you will create an Array with 5 boolean values which by default are set to false. Given the array is in arrays() methods scope, you can pass it to another method only from arrays() method. – e.doroskevic Nov 17 '15 at 15:33
  • As @AdelBoutros suggests, update an adjacent component in the listener to another component's model, as shown [here](http://stackoverflow.com/a/13919878/230513). – trashgod Nov 17 '15 at 17:35

4 Answers4

1

To give you are starting hand... you can set the result of your arrays method to a local variable in the main method and pass as a argument to the seen. Then you can do the same for the display method.

    public static void main (String[]param)
    {   
        boolean[] birds = arrays();
        seen(birds); 
        display(birds);
    }

    public static boolean[] arrays()
    {
    ...
    }
    public static boolean seen(boolean[] birds)
    {
    ...

There are plenty of tutorials around the web for this kind thing. Here being one example.

George Lee
  • 826
  • 6
  • 11
1

You need to pass it as a parameter or declare a global array.

  1. Passing by parameter:

class methodarrays {

public static void main (String[]param)
{
    boolean [] myArray =arrays();
    seen(myArray); 
    display(myArray);
}

public static boolean seen(boolean [] myArrayParam)
{
   for (int i=0;i<myArrayParam.length;i++)
   {...}
}

public static boolean display(boolean [] myArrayParam)
{
   for (int i=0;i<myArrayParam.length;i++)
   {...}
}

}

  1. As global array:

class methodarrays {

   boolean [] myArray

public static void main (String[]param)
{
    myArray = arrays();
    seen(); 
    display();
}

public static boolean seen()
{
   for (int i=0;i<myArray.length;i++)
   {...}
}

public static boolean display()
{
   for (int i=0;i<myArray.length;i++)
   {...}
}

}

user1697575
  • 2,830
  • 1
  • 24
  • 37
1

Declare

boolean [] birds = new boolean [5];

as accessible object for all methods within your class.

import javax.swing.*;

class methodarrays
{   

    private boolean [] birds = new boolean [5]

    ...

    public static boolean[] arrays()
    {
        for (int i=0;i<birds.length;i++)
        {birds[i]=false;
        }
        return birds;
    }

    ...
}
Farasy
  • 304
  • 2
  • 8
0

Here is the implementation mimicing your own:

import javax.swing.JOptionPane;

public class Example { private static boolean [] birds = new boolean [5];

   public static void main (String[]param){ 
       arrays();
       seen(); 
       display();
   }

   public static boolean[] arrays()
   {   
       // Completely unnecessary since values are set to false by default;
       for (int i=0;i<birds.length;i++)
       {birds[i]=false;
       }
       return birds;
   }
   public static void seen(){   
       String quit = "100";
       String ans = "";
       while(!ans.equals(quit))
       {
           ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit   2) Blackbird   3)Robin   4)Wren   5)Greenfinch");
           if (ans.equals("1"))
           {   birds[0] = true;
           }
           else if (ans.equals("2"))
           {   birds[1] = true;
           }
           else if (ans.equals("3"))
           {   birds[2] = true;
           }
           else if (ans.equals("3"))
           {   birds[2] = true;
           }
           else if (ans.equals("4"))
           {   birds[3] = true;
           }
           else if (ans.equals("5"))
           {   birds[4] = true;
           }
       }
   }

   public static void display(){
       System.out.println("Your results are: ");
       System.out.println("Blue Tit: " + birds[0]);
       System.out.println("Blackbird: " + birds[1]);
       //and so on..
   }

}

e.doroskevic
  • 2,129
  • 18
  • 25
  • It should be mentioned, the code above could be reduced substentially. However, the code has been kept to reflect the level of proficiency displayed in the OPs question. – e.doroskevic Nov 17 '15 at 15:47