Results 1 to 3 of 3
  1. #1
    agente47 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default ERROR: Calling array from a method

    hello everyone. am new to java. i want to create a battleship kind of program in which i randomly generate 4 ships. each ship comprise of four "*" and hide it.

    now the problem is i made a method and called that method for placing randome ships and hiding it but its not working. am not too bright in this but could someone help me please as far as u can? thanx for reading and replying.

    import java.util.Random;
    import java.util.Scanner;
    public class mar25
    {
    char[][] player1Grid;
    int x, y; // x and y axis


    public void main(String[] args)
    {
    char[][] playerGrid=new char [14][12];

    Scanner myKeys = new Scanner(System.in);


    for(int playerCount = 0; playerCount <2; playerCount++)
    {
    for(int a=0; a<14; a++) // 14 rows
    {
    for(int b=0; b<12; b++) // 12 columns .
    {
    playerGrid[x][y]='*';
    playerGrid[x][y]='*';
    }
    }
    }
    shi();
    }

    /////////////////////////////////// method
    public char shi()
    {
    char[][] playerGrid=new char [14][12];
    Random anyNum = new Random();
    for(int playerCount = 0; playerCount <2; playerCount++)
    {
    x=anyNum.nextInt(14);
    y=anyNum.nextInt(9);
    playerGrid[x][y] = 's';
    playerGrid[x][y+1] = 's';
    playerGrid[x][y+2] = 's';
    playerGrid[x][y+3] = 's';
    }

    for(int a=0; a<14; a++)
    {
    for(int b=0; b<12; b++)
    {
    if(playerGrid[x][y] == 's')
    {
    System.out.print('d');
    }
    else {
    System.out.print(playerGrid[x][y]);
    }
    }
    }
    return playerGrid[x][y];
    }
    }
    i was in hurry posting this thread so pls avoid any stupidnes ;)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    what exactly is wrong with your method - what happens that is not meant to happen?

    Java Code:
    playerGrid[x][y] = 's';
    playerGrid[x][y+1] = 's';
    playerGrid[x][y+2] = 's';
    playerGrid[x][y+3] = 's';

    you havent taken into account (above) if the ship starts near the bottom, y+1, y+2, or y+3 could go out of range.

  3. #3
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    Hi there:
    Next time please try to surround your code with the [ code ] [ / code ] tags as that will retain indentation and make the code easier to read and follow, especially in your case where you have nested loops.

    What do you mean you want to hide them? i am not quite sure what you are trying to accomplish here.

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. Replies: 10
    Last Post: 09-18-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •