---
title: "How to prevent a user from going out of bounds in simple array game."  
description: "How to prevent a user from going out of bounds in simple array game."  
author: "Royce Roy"  
published: 2015-04-30  
updated: 2018-02-06  
canonical: https://www.mindstick.com/forum/23164/how-to-prevent-a-user-from-going-out-of-bounds-in-simple-array-game  
category: "java"  
tags: ["java"]  
reading_time: 7 minutes  

---

# How to prevent a user from going out of bounds in simple array game.

I am making a very simple [2D array](https://www.mindstick.com/forum/1796/how-to-convert-jagged-array-to-2d-array) game where the player is asked what size they would like the game board to be. After entering, it displays the board and the player 'P' starts at index [0][0]. After that, they are asked for an action, which can be "up", "down", "left", "right", or "exit". I will be including some extra later (like a [treasure](https://yourviews.mindstick.com/story/5125/shri-padmanabhaswamy-temple-home-of-untouched-treasure) at the end, or random obstacles), but for now this is what the game consists of. \
I was instructed to "do nothing" when/if the player attempts to go out of bounds. I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to simply print an error, such as "Out of bounds! Try again.", then prompt the player again for an action. I even tried to make a [boolean](https://www.mindstick.com/forum/160332/how-does-the-boolean-function-work) method to catch it, but to no avail. \
I don't want the [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) to occur at all. I just simply want the [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) to print to the player and ask for another action. I would prefer not to use try/catch, or try/catch/finally since I [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) tried that and it still gave the exception error. \
This [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) consists of two classes. I will show the class containing the main first, then the client-[server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) type class second. \

```
import java.util.Scanner; public class Driver {                   public static void main(String[] args) {               World world = new World();       boolean keepPlaying;       keepPlaying = true;       boolean isOutOfBounds;       isOutOfBounds = false;       int height = 0;       int width = 0;       int x = 0;       int y = 0;               Scanner input = new Scanner(System.in);               System.out.print("Enter World width: ");       width = input.nextInt();       System.out.print("Enter World height: ");       height = input.nextInt();               world.displayWorld(height, width, x, y);               input.nextLine();               while(keepPlaying) {               System.out.print("ACTION > ");       String action = input.nextLine();               if(world.isOutOfBounds()) {                       System.out.println("Out of bounds! Try again.");                       System.out.println("ACTION > ");           input.nextLine();                    }       else if(action.equalsIgnoreCase("down")) {           world.moveDown(height, width, x, y);                    }       else if(action.equalsIgnoreCase("up")) {           world.moveUp(height, width, x, y);                    }       else if(action.equalsIgnoreCase("left")) {           world.moveLeft(height, width, x, y);        }       else if(action.equalsIgnoreCase("right")) {           world.moveRight(height, width, x, y);        }       else if(action.equalsIgnoreCase("exit")) {           System.out.println("Thanks for playing. Goodbye!");           world.exitGame();        }    }                                          }//end main}//end class
```

\
\
HERE IS THE SECOND CLASS: \

```
class World {         private int characterRow;    private int characterColumn;    private char[][] theWorld;     char player = 'P';    int playerX = 0;    int playerY = 0;         public World() {                 characterRow = 0;        characterColumn = 0;    }    public World(int height, int width, int x, int y) {                 this.characterRow = height;        this.characterColumn = width;        this.playerX = x;        this.playerY = y;    }    public void setPlayerX(int x) {        x = 0;        playerX = x;    }    public void setPlayerY(int y) {        y = 0;        playerY = y;    }    public int getPlayerX() {        return playerX;    }    public int getPlayerY() {        return playerY;    }    public boolean isOutOfBounds() {        boolean isOutOfBounds = false;                 for(int i = 0; i < theWorld.length-1; i++)        {            for(int j = 0; j < theWorld[i].length-1; j++)            {                if(playerX < 0 || playerY < 0 || playerX > theWorld.length-1 || playerY > theWorld.length-1) {                    isOutOfBounds = true;                }            }        }        return isOutOfBounds;              }    public void moveUp(int height, int width, int x, int y) {        x--;        playerX = playerX + x;        displayWorld(height, width, x, y);    }    public void moveDown(int height, int width, int x, int y) {                x++;        playerX = playerX + x;        displayWorld(height, width, x, y);    }    public void moveLeft(int height, int width, int x, int y) {        y--;        playerY = playerY + y;        displayWorld(height, width, x, y);    }    public void moveRight(int height, int width, int x, int y) {        y++;        playerY = playerY + y;        displayWorld(height, width, x, y);    }    public void exitGame() {                 System.exit(0);    }    public void displayWorld(int height, int width, int x, int y) {        theWorld = new char[height][width];        for(height = 0; height < theWorld.length; height++)        {            for(width = 0; width < theWorld[height].length; width++)            {                                 theWorld[height][width] = '-';                 theWorld[getPlayerX()][getPlayerY()] = player;                                 System.out.print(theWorld[height][width] + " ");            }            System.out.println();        }    }//end displayWorld}//end class
```

\
\
PLEASE HELP. This is driving me nuts. Thanks in advance. :-) \

## Replies

### Reply by Han Jenny

Post is removed by the Admin.

### Reply by Anonymous User

Ok, I gave in and added a try/catch method and it worked since Java is cranky. Now I suppose I have one final question. Is there a way to implement code to make the user not have to type the opposite direction twice to get back "in bounds"?\
\
For example, if I reach the end of the array going "down", it prints the [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) and asks to "try again". However, I have to type "up" twice (once to get back in bounds, and again to actually move up).\
\
Here is the new code:\

```
while(keepPlaying) {           try {               System.out.print("ACTION > ");       String action = input.nextLine();              if(action.equalsIgnoreCase("down")) {           world.moveDown(height, width, x, y);                    }       else if(action.equalsIgnoreCase("up")) {           world.moveUp(height, width, x, y);                    }       else if(action.equalsIgnoreCase("left")) {           world.moveLeft(height, width, x, y);        }       else if(action.equalsIgnoreCase("right")) {           world.moveRight(height, width, x, y);        }       else if(action.equalsIgnoreCase("exit")) {           System.out.println("Thanks for playing. Goodbye!");           world.exitGame();        }    }catch (ArrayIndexOutOfBoundsException e) {    world.isOutOfBounds();    System.out.println("Out of bounds. Try again.");}}
```

\


---

Original Source: https://www.mindstick.com/forum/23164/how-to-prevent-a-user-from-going-out-of-bounds-in-simple-array-game

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
