How To Prevent A User From Going Out Of Bounds In Simple Array Game
Apr 26, 2015
I am making a very simple 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 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 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 method to catch it, but to no avail.
I don't want the exception to occur at all. I just simply want the error message 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 tried that and it still gave the exception error.This program consists of two classes. I will show the class containing the main first, then the client-server 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;
The question pretty much says it all. My problem seems to be when adding the neighbours, I am always getting a Index out of Bounds problem. I know this is because the code is reaching for the edge of the table, for example if the column, i = 0 and the statement says to perform i - 1 and return it, then we are going to have a problem.
Similarly with anything like j = 20, j + 1 (as the grid only has 20 spaces). I understand the problem, but I am unsure of how to solve it. I have tried messing around with the if statements, but I continue to get the 'out of bounds' problem...
import javax.swing.*; public class GameOfLife { /** * @param args the command line arguments */ public static void main(String[] args) { int cellChoice = 0; int newCells = 1; int generation = 1; int neighbours = 0;
I have a TableCell that will hold numbers in a tableview. All is working work nicely, but I want the following behavior:
- when the user begins to edit such a cell, if it doesn't enter a number, the cell will not call commitEdit, but rather display a red border and prevent the user from changing the focus to anything else until he either: enters a correct number or presses ESC.
I don't know how to keep the user in that editting cell if while he has an incorect number. Currently he can click other row/control and he will break the edditing state. I repeat, I don't want the user to be able to click on any row/control until he has a correct number.
Here is my cell implementation:
public class EditableIntegerCell extends TableCell<Person, Integer> { private TextField textField; @Override public void startEdit() { if (!isEmpty()) { super.startEdit(); createTextField(); setText(null);
import java.util.*; public class DungeonsAndDragonsRedux { public static void main(String[] args) { Scanner s = new Scanner (System.in); Scanner t = new Scanner (System.in); t.useDelimiter("\n"); // Prevent scanner from reading code after nextLine().
[code]...
I have this program that is attempting to randomly equip the player with items that are randomly chosen from a list. I don't know how to prevent it from picking the same item twice though. I also don't know how to display the items that haven't been equipped, which is another requirement of this program.
Goal this time is to take a charArray, copy it into another charArray while reversing the things in it.
E.g. charArray["!ollaH"] into charArrayNew["Hallo!"]
My first idea was to revert the stuff in the Array with a ! cause i saw earlier that u can work with that too revert booleans. Sadly i didnt happen to make it work.
Next thing i thought of was a for loop to go trough the charArray and copy every section into charArrayNew just at the opposite end.
Java Code:
import java.util.Arrays; public class aufgabe43 { public static void main(String[] asgr){ char[] charArray
[Code] .....
Eclipse doesn't show any errors, and as u told me last time i did include import java.util.Arrays; to output the array in the end.
When i try to compile the code eclipse returns with an error
Java Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 68 at aufgabe43.main(aufgabe43.java:8) mh_sh_highlight_all('java');
Which I frankly don't understand since the array . Length is exactly the same.
When I ran the methods "go1,go2,go3" separately without clicking on the menu , all of them worked well. by separately I mean, when I closed the program changed the source code from "go1;" to "go2" and go3. But when I click on the "change ball" and then "1 ball" or any thing, all I get is a new frame which is blank. And the old frame freezes.
I am trying to print out all of the values of an array and the average. I am getting part of the printout but I am also getting an array out of bound error message. what I am doing wrong?
int [][] points = new int [2] [3]; points [0][0]= 3; points [0][1]= 2; points [0][2]= 4; points [1][0]= 2; points [1][1]= 2; points [1][2]= 2; int totalPoints = 0; int totalShots = 0;
It seems that all of my arrays in the class Calculations are out of bounds, and I do not seem to know why.
Java Code:
/** public class DataAnalyzer { public static void main (String[] args) { //This creates an instance of ReadFiles ReadFiles aReadFiles = new ReadFiles(); Calculations aCalculations = new Calculations();
I have been working on this program for a while and now i seem to be stump it throws an outof Bound array exception error, this program is a matrix multiplication program and spits out the resulting matrix using multithreading. i have a running one and result is
2 -1 0 1 0 3 -1 1 3
but this program's result is:
2 0 0 1 0 0 -1 0 0
it reads a txt document as an commandline arguement the text file reads just like this below:
3 2 2 3 1 0 0 1 -1 1 2 -1 0 1 0 3
the following is my code:
import java.util.*; import java.io.*; public class P3 { public static int matrix1[][]; public static int matrix2[][];
I am using libgdx and box2d to try to make a simple cannon game.
When I have the cannonball collide with another object I want to delete that object and have it stop calling all of it's functions. It doesn't seem like I can have it delete itself, and the contactListener has a reference to the cannonball and other object, but if I set either to null, it seems the original reference still exists and the object isn't deleted.
I don't know how to write a code to pong ball, when it touch red block.
public class bounc2 extends JPanel implements ActionListener,KeyListener{ double x = 0,y=0,velX=3,velY=3; double x2 = 5,y2=145,velX2=0,velY2=0; double x3= 473,y3=145,velX3=0,velY3=0; Timer t = new Timer(5,this); public bounc2(){ setBackground(Color.GREEN);
class test{//class public static void main(String[]args) { String booking [][]= new String [30] [6] ;//two dimensional array System.out.println("Enter the seat column you want");//column entry char column=Keyboard.readChar(); System.out.println("Enter the seat row you want");//row entry int row=Keyboard.readInt();
I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code.
Java Code:
public class bubbleSort { public static void main(String []args) { int i; int array [] = {12,9,4,99,120,1,3,10};
I have a 2-D character array and I was wondering if I can compare a given index to an out of bounds index and what would I have to write? For instance
if(array[0 - 1][0 - 1] == 'indexoutofbounds'){ **// I know that 'indexoutofbounds' is probably not the right wording** stuff happens }
If I can do this what would I actually have to write in where it says 'indexoutofbound' ?
I will be using this for many indexes so it wont always be [0 - 1][0 - 1] it could be [4 - 1][3 - 1] in which case it wouldn't be out of bounds and wouldn't move onto "stuff happens"....
I have two codes of needed to run but after processing, the said above error is preventing it from getting the general output of the code.I can't seem to find my errors or I don't have any clue at all, if perhaps you know, here's the codes:
Error : PHP Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at simpleOutput.main(simpleOutput.java:13) public class simpleOutput { public static void main(String args[]){ int n = Integer.parseInt(args[0]); for(int i = 1; i <= n; i++){ } for(int i = 1; n <= i; n++){ System.out.print((i*n)+" ");
I am writing code for a simple java game. The problem I am running into is that I have a moving icon (J button) that is controlled using the keyboard (arrows) and I also have stationary icons (J button). Is there a way to keep the moving icon from passing over the stationary icon? I would like the stationary icon to act like a wall.
The game is too have two players, each user clicks a button and two dices will roll, if a user rolls a double they win.
firstly I have started with two imageview's and a button I am trying to randomise two images by clicking one button I have managed to randomise one image in one box but I am struggling to randomise both image views here is my code so far.
I am trying to make a simple platformer game in Java. When I try to move the character I made it only moves over then immediately back
import java.awt.event.KeyEvent; public class UCC { private int xLoc; private int yLoc; private int xSpeed; private int ySpeed; private double gravity; private int size;
I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?
import java.io.*; import java.util.*; public class Driver { public static void main(String[] args) { new Driver(args[0]);
[Code] ....
I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?
I tried this program, its guessing game. Where program decide 1 number and user will guess number. I am getting 1 problem. When user want to play game again.the random number remain same. So where i can put reset random in code..? And 1 more question if I want to write driver code for this. What should i transfer to driver code.
import java.util.Random; import java.util.Scanner; public class GuessGame { public static void main(String args[]) { String choice="y";