-1

I am very new to Java, and I'm having a difficult time figuring out how to take arguments from the command prompt and pass them around in my code. I am able to get them into the main method of my code, but I'd rather have them in the Chessboard class. There is a public static int n that is hard coded, but I would like it to be whatever I send in as arguments. I'll later be taking an initial position for a queen's placement, so I'm hoping the process will be similar; if I get help with this, hopefully I can use the same technique for that.

public class Chessboard {

public static void main(String[] args) {


    System.out.println("Hello World");
    Chessboard board = new Chessboard();    //creates a Chessboard object
    board.start();                          
}

public  static int n = 8;                                   
private static int board[][];    //this is the Chessboard array
private int numQueens;    //this is the number of queens on the board


public Chessboard(){
    numQueens = 0;    //initialized to zero, no queens on board to start yet
    board = new int[n][n];    //nxn 2D array of zeros
    for (int j = 0; j < n; j++)
    {
        for (int k = 0; k < n; k++)
        {
            board[j][k] = 0;    //redundant, but I need to learn how to 
        }                       //initialize. this manually puts zeros into 
    }                           //the array
}

...and the code continues from here, but I don't think it's necessary. If it is, I'm happy to upload it.

Thank you for your time.

choblet
  • 1
  • 1
  • Possible duplicate of [how to pass command line arguments to main method dynamically](http://stackoverflow.com/questions/8756377/how-to-pass-command-line-arguments-to-main-method-dynamically) – dumbPotato21 Jan 20 '17 at 04:55
  • I don't believe it's a duplicate of that question. – Dawood ibn Kareem Jan 20 '17 at 04:57
  • `static` is not a cross object communication mechanism and you should learn to live without it as soon as you can. Maybe start by having a look at [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Jan 20 '17 at 04:58
  • `Chessboard board = new Chessboard(Integer.parseInt(args[0]));` – Robby Cornelissen Jan 20 '17 at 04:59
  • 1
    possible dup http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – Scary Wombat Jan 20 '17 at 05:00

3 Answers3

1

Here's what I'd do.

public static void main(String[] args) {
    try {
        int firstArg = Integer.parseInt(args[0]);
        Chessboard board = new Chessboard(firstArg);

        // Do some stuff with the chessboard here.

    }
    catch(NumberFormatException e) {
        System.out.println("That's not a number");
    }
}

This looks at the first command line argument and tries to convert it to an int. If it succeeds, it passes that int to the constructor of Chessboard to make an object for you to use.

This snippet also shows how you can provide code that runs if the first command line argument isn't actually a number.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thank you for the information. I've been playing with it for a while, and I am having some problems... Since the argument 'firstArg' is getting passed to the constructor, do I need to specify that it will be receiving an 'int' parameter in the parenthesis, like 'int firstArg'? I'm also having compiling errors with the rest of my code (another seven methods) that use the variable 'n' that I can't seem to fix. Is there any way to pass 'firstArg' to my 'public static int n'? – choblet Jan 20 '17 at 06:29
  • You had the right idea when you had `public Chessboard (int n)`. I'm not sure why you changed your question after that. – Dawood ibn Kareem Jan 20 '17 at 07:06
  • I finally got it figured out. In the main class I used `Chessboard.n = Integer.parseInt(args[0])`, and in the `public class Chessboard` i created `public static int n;`. Thank you for the help. Even though I didn't use it exactly, it pointed me in the right direction. – choblet Jan 21 '17 at 05:03
0

Notice that the main method is already in your Chessboard class. If you want to leave your n variable as static, you can just do this in the main method.

n = Integer.parseInt(args[0]);

If you make n an instance variable instead of having it be static, then the answer that David Wallace gave will point you in the right direction.

Michael Sharp
  • 496
  • 3
  • 10
-1

In your Chessboard Class create an

private String[] args;

Then add an setter to Chessboard like:

public void setArgs(String[] args{
    this.args = args;
}

Then put something like this in your main:

public static void main(String[] args) {

    Chessboard board = new Chessboard();
    board.setArgs(args);
    board.start();                                          
}
kookej
  • 9
  • 3