as others suggested you should use 2 for loops. I've added the required logic get the result you're looking for
public void look()
{
StringBuffer sb = new StringBuffer(25);
for(int y= posY-2; y <= posY+2; y++){
for(int x = posX-2; x <= posX+2; x++){
try
{
if(x<0 || y<0 || x > myWorld.length || y > myWorld[x].length)
sb.append("#");//Wall or outside map
else
if(Math.abs(posX-x)+Math.abs(posY-y) < 4)
sb.append(myWorld[x][y]); //Valid part of map
else
sb.append("X"); //Unreachable corner
}
catch(IndexOutOfBoundsException e){} //This shouldn't happen :)
}
sb.append("\n");
}
System.out.println(sb);
}
Using StringBuffer will keep the execution time down with bigger amounts of data