How to pass an array and an int trough classes and methods?

I am trying to make the compiler pass the array from one of the classes to the main method. I don't know why it does not work, the code looks like this:

This is my main method -

public class Main {
    public static void main(String[] args) {

        int[] board2;
        int userInput;
        playBoard = methods.createBoard();
        userInput = methods.input();



    }

}

And this is my methods class -

import java.util.Scanner;

public class methods {

        //Create board method

    int[] createBoard()
    {
        int[] board = new int[7];
        int randomNum =(int) (Math.random()*5);
        for (int i=0; i<2; i++)
        {
            board[randomNum+i] = 1;
        }
        System.out.println("Board created");
        return board;
    }

        //Take a guess method


    int input()
        {
            int input=0;
            Scanner reader = new Scanner(System.in);
            System.out.println("Please enter your guess now");
            input = reader.nextInt();
            System.out.println("Guess entered successfully");
            return input;
        }

}

I am aware of that I could easily put everything in one class and even one method but i'm to practice on using classes and methods therefore I create lots of them.

Answers


You'll have to create a new instance of Main and methods first or alternatively declare the createBoard() and input() methods static.

Here is the code snippet:

public class Main {
    public static void main(String[] args) {
        Main m = new Main();
        m.run();
    }

    private void run() {
        methods me = new methods();
        int[] playBoard = me.createBoard();
        int userInput = me.input();
    }
}

Also, as per the naming convention rules for the class name it should be Methods instead of methods.


You haven't declared the variable playBoard being used inside Main. Did you intend to use board2 instead. I guess you want something like below:

board2 = new methods().createBoard();
userInput = new methods().input();

You need to create an object of class methods, in order to access instance methods.


Need Your Help

sails can't find layout.jade

node.js pug sails.js

I am starting to learn Sails (0.9.7, node 0.10.16) and running through the sailscasts episodes. I am also trying to use jade as I do so. Where I am stuck now is that sails is not finding views/layout.

Odd Regular Expression

regex

I'd like to create a regular expression in perl that can find an SSN in a given text file while not matching a nine digit number preceded by specific text. The expression I have: