1

How can I access my array from a different class? I have 3 classes; Main (where I want to access the array from) FramePanel (my GUI and where the value from UserInputNum is taken from) and StoryArray (where my array is saved).

I need to access the array in the nested If loop in the Main class, this is because I want too save the specific array data to a string and eventually append it into a JTextArea.

Here are the two classes needed:

Main.java

public class Main
{
    public static String UserInput;
    public static int UserInputNum;

    public static void main(String[] args)
    {
        FramePanel.main();
        StoryArray.main();

        UserInputNum = Integer.parseInt(UserInput);
        if (UserInputNum >= 0)
        {
            if (UserInputNum <= 399)
            {
                StoryArray.storyLine[UserInputNum];
            }
            else
            {

            }
        }
        else
        {

        }
    }
}

StoryArray.java

public class StoryArray
{
    public static String storyLine[] = null ;
    public String[] getStoryLine()
    {
        return storyLine;
    }
    public static void main()
    {
        //String[] storyLine;
        storyLine = new String[399];
        storyLine[0] ("1")
        storyLine[1] ("2")
        storyLine[2] ("3")
        storyLine[3] ("4")
        storyLine[4] ("5")
        storyLine[5] ("6")
2
  • 2
    StoryArray.storyLine + the class is Main, not main ... Commented Jan 7, 2016 at 23:19
  • I've added in how i think it should be but i still get a syntax error saying "AssignmentOperatior Expression" to complete expression Commented Jan 7, 2016 at 23:24

3 Answers 3

2

In another class you can call the array like this:

 String value = StoryArray.storyLine[index];
Sign up to request clarification or add additional context in comments.

Comments

1

As it is a static public field you can access it directly by StoryArray.storyLine. But as you have a getter ethod I would suggest to make this getter setter static and the array field private and access it through getter method like that: StoryArray.getStoryLine() (to see why read about encapsulation).

You also shouldn't start your class (main) name from lower case, here are standard coding conventions for java language: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

1 Comment

i tried StoryArray.getStoryLine(storyLine[UserInputNum]); but it returns an error saying storyLine cannot be resolved to a variable, what does this mean? sorry I've tried using getters which is why one is there but everything i try kicks back an error, i want to be able to use UserInputNum as the number of the cell in the array - Thanks,
1

Once you've called StoryArray.main(), then you should be able to do StoryArray.storyLine[/*element id*/] = "whatever you want" to get or set any element in storyLine. Additionally, you aren't defining any default array values. In StoryArray.main(), you need to have lines of the form storyLine[n] = "n".

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.