JimiiBee
JimiiBee

Reputation: 159

Getting coordinates for 2D Array within ONE function

How can I retrieve X and Y coordinates from a user within one function?(For java game needing coordinates for array)

"The getX function will ask the user for an X coordinate. It will return the number once entered by the user. getY has the same function, the message is adjusted to ask for the Y coordinate."

This is what I need to follow by for the function. The return values need to be used to place a number in a 2D Array. How do I achieve this in one function?

board[x][y] = 1;

EDIT: I can only do some much within java (for computing test, so marks won't be awarded for using anything out of what we have been taught - AQA AS Level). I need to scan inputs from the user in the function and return both coordinates to be used by the 2D array in main.

Please tell me if I am being confusing or I am not making sense and I'll try and explain better.

If there is no way to do it this way, just tell me. JimiiBee

Upvotes: 0

Views: 2777

Answers (2)

user3123890
user3123890

Reputation:

There are great resources here on actually getting user input. Check out this post. to create the array I would make something like this:

public int[] getCoord(int[] coords) {
    coords[0] = getInt("Please enter x coordinate:");
    coords[1] = getInt("Please enter y coordinate:");
    return coords;
}

This would be called like this:

coords = getCoord(coords);

Which would replace the old value of coords with the new values.

A getInt method would look like this:

private int getInt(String prompt) {
    Scanner scanner = new Scanner(System.in);
    System.out.println(prompt);

    int in = scanner.nextInt();
    scanner.close();
    return in;
}

Of course this could be consolidated into a single method that avoids repeated opening and closing of a scanner, but if you are using getInt() elsewhere in the code, this may still be a preferred solution.

If you're forced into using separate methods, we'd rather not have to open and close the scanner repeatedly, instead we'll pass the scanner in from the getCoord class.

public int[] getCoord(int[] coords) {
    Scanner scanner = new Scanner(System.in);
    coords[0] = getX(scanner, coords);
    coords[1] = getY(scanner, coords);
    scanner.close()
    return coords;
}

And an example get method:

private void getX(Scanner scanner, int[] coords) {
    System.out.println("Please enter x coordinate:");
    coords[0] = scanner.nextInt(); //Change 0 index to 1 for getY
}

Upvotes: 1

blackcompe
blackcompe

Reputation: 3190

You can return a 2-element array, concatenate the values into a string separated by a delimiter, or create a Dimension class that contains x- and y- coordinates.

String promptCoords()
{
    return promptX() + ":" + promptY();
}

int[] promptCoords()
{
    return new int[]{promptX(), promptY()};
}

Dimension promptCoords()
{
    return new Dimension(promptX(), promptY());
}

private class Dimension 
{
    int x, y;

    Dimension(int x, int y) 
    { 
        this.x  = x;
        this.y = y;
    }
}

int promptX(){return -1;}
int promptY(){return -1;}

Upvotes: 1

Related Questions