Reputation: 194
I have an array:
public String[][] bombBoard = new String[9][9];
This array is filled with values (*), however randomly throughout there are some other values (@).
Later in the program I want to tell the user how where the (@) is, if there are in the vicinity:
public void loadNumbers()
{
try
{
if (bombBoard[bombRow][bombCol] == "@")
{
System.out.println("You have landed on a bomb!");
//break
}
else if (bombBoard[bombRow+1][bombCol].equals("@"))
{
System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow+1][bombCol]);
}
else if (bombBoard[bombRow-1][bombCol] == "@")
{
System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow-1][bombCol]);
}
else if (bombBoard[bombRow][bombCol+1] == "@")
{
System.out.println("There is a bomb in the vicinity!" + bombBoard[bombRow][bombCol+1]);
}
MORE CODE...
}
}
It prints: "There is a bomb in the vicinity!@"
I want it to print "There is a bomb at 3, 2"
Probably so simple, but I'm drawing blanks. Instead of pulling back whats inside the element, I want the element index (I presume). Please halp!
Upvotes: 0
Views: 46
Reputation: 5802
try
System.out.println("There is a bomb in the vicinity!" + (bombRow+1).toString() +"," +bombCol.toString());
Upvotes: 0
Reputation: 95528
I assume you just want to print out the coordinates of the @
? If so you can do it like this:
System.out.println("There is a bomb at " + bombRow + ", " + bombCol + 1);
Use the same pattern for the other conditions. Also, you want to compare strings using .equals
instead of ==
(the latter only compares references).
Upvotes: 1