0

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

JimiiBee
  • 159
  • 4
  • 14

2 Answers2

1

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;}
blackcompe
  • 3,180
  • 16
  • 27
  • Sorry I should of clarified, I can only use so much within java for the game(its apart of my computing course test). So I don't believe I can use a Dimension, I need to scanner the x and y coordinate from the user, and return those values to be entered in the 2D array already made. – JimiiBee Jan 17 '14 at 18:39
1

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
}
Community
  • 1
  • 1
  • The 2D array has already been made, the function just needs to place a value in the coordinate put in by the user through a scanner into the array. – JimiiBee Jan 17 '14 at 18:46
  • Then instead of creating a new array, pass in the old one! I'll edit now to show what I mean. –  Jan 17 '14 at 18:48
  • I understand. I do need to make two functions in order to get the right coords for a 2D array then. This would work only for a 1d array right? I'll accept your answer any ways because it has made me understand what to do. – JimiiBee Jan 17 '14 at 18:56
  • I'll edit to show a solution that avoids having to repeat the input code. Good Question. –  Jan 17 '14 at 18:57
  • Note the quotation in my question, as this is all I can follow by at the moment – JimiiBee Jan 17 '14 at 18:59
  • I've showed the consolidated solution. If you're forced to follow teacher's silly conventions then do so by creating 2 separate `getx` and `gety` methods. It's not worth fighting. I'll edit one final time to show a solution that avoids repeated scanner opening/closing even with 2 methods. –  Jan 17 '14 at 19:05
  • Ahh okay, thank you. I'm not given much information on how to do this code, but I think two methods are the best way to do this. – JimiiBee Jan 17 '14 at 19:18
  • Your question has changed significantly since the first post. Careful as I have used a 1 dimensional array, and your OP now describes a 2 dimensional array. You'll have to update my methods slightly to deal with this. –  Jan 17 '14 at 19:19
  • I thought my first post stated it was for a 2D Array. Sorry about that – JimiiBee Jan 17 '14 at 19:28