1

What I'm trying to do is generate a 15x15 square of "-" and accept a user input coordinate which will then replace the "-" with a "x" currently my program is just printing a vertical line of "-"

import java.util.*;
public class GameOfLife
{
    public static void main(String[] args)
    {
        int[][] boardList = new int[15][15];
        Scanner myScanner = new Scanner(System.in);
        boolean done = false;
        do
        {
            System.out.println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
            int choice = Integer.parseInt(myScanner.nextLine());
            if (choice == 1)
            {
                System.out.print("Enter the x coordinate: ");
                String answer = myScanner.nextLine();
                int xCoordinate = Integer.parseInt(answer);
                System.out.print("Enter the y coordinate: ");
                String answer2 = myScanner.nextLine();
                int yCoordinate = Integer.parseInt(answer2);
                for(int i = 0; i < 15; i++)
                {
                    for(int j = 0; j < 15; j++)
                    {
                        if(xCoordinate == i)
                        {
                            if(yCoordinate == j)
                            {
                                System.out.printf("x", boardList[i][j]);
                            }
                        }
                        else
                        System.out.printf("-", boardList[i][j]);
                        System.out.println();
                    }
                }
            }
        }
    }
}
6
  • 1
    What do you understand by System.out.printf("x", boardList[i][j]);. Commented Apr 24, 2014 at 19:43
  • 1
    where is while of do loop. Commented Apr 24, 2014 at 19:46
  • I used System.out.printf to get it to print earlier without trying to add coordinates in Commented Apr 24, 2014 at 19:46
  • The while for the do loop is down later in my choice 6. I didn't feel like it was necessary to add it in because it didn't pertain to the error Commented Apr 24, 2014 at 19:48
  • it seems like your mixing up code intended to assign values to boardlist and code that is intended to print boardlist. The way it is now, boardlist is not used for anything at all. Commented Apr 24, 2014 at 19:51

4 Answers 4

2

here you have , this works ... u need to put System.out.println(); outside inner loop as well as put

if(xCoordinate == i){
  if(yCoordinate == j){

to one condition ...

 public static void main(String[] args) {
            int[][] boardList = new int[15][15];
            Scanner myScanner = new Scanner(System.in);
            boolean done = true;
            do {
                System.out
                        .println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1) {
                    System.out.print("Enter the x coordinate: ");
                    String answer = myScanner.nextLine();
                    int xCoordinate = Integer.parseInt(answer);
                    System.out.print("Enter the y coordinate: ");
                    String answer2 = myScanner.nextLine();
                    int yCoordinate = Integer.parseInt(answer2);
                    for (int i = 0; i < 15; i++) {
                        for (int j = 0; j < 15; j++) {
                            if (xCoordinate == i && yCoordinate == j) {
                                System.out.printf("x", boardList[i][j]);
                            } else
                                System.out.printf("-", boardList[i][j]);
                        }
                        System.out.println();
                    }
                }
            } while (done);

        }

//EDIT note that i changed done to true just to demonstrate that it works ...

Sign up to request clarification or add additional context in comments.

Comments

1

try this

for(int i = 0; i < 15; i++)
{
    for(int j = 0; j < 15; j++)
    {
        if(xCoordinate == i && yCoordinate==j)
            System.out.printf("x", boardList[i][j]);
        else
            System.out.printf("-", boardList[i][j]);
    }
    System.out.println();
}

You need to print the new line after you finish printing a whole row first

Comments

0

If I understand well, You want a two dimensional array initialized with '-', to do so you could do

int[][] boardList = new int[15][15];

for (int row = 0; row < 15; row ++)
    for (int col = 0; col < 15; col++)
        boardList[row][col] = '-';

And then, once you store the user data in xCoordinate and Ycoordinate, you simple do:

boardList[xCoordinate][Ycoordinate] = 'x';

2 Comments

boardlist is an int[][], not a char[][]
@matrixugly ups, I get confused when he said a 15x15 square of "-". Thanks
0

You can try this code:

import java.util.Scanner;


public class StackOverflow 
{
    public static void main(String[] args)
    {
        int[][] boardList = new int[15][15];
        Scanner myScanner = new Scanner(System.in);
        boolean done = false;
        System.out.println("1 - Add a being \n 2 - Show current board \n 3 - Show next generation \n 4 - Clear board \n 5 - Add preload pattern \n 6 - Exit");
        int choice = Integer.parseInt(myScanner.nextLine());
        if (choice == 1)
        {
            System.out.print("Enter the x coordinate: ");
            String answer = myScanner.nextLine();
            int xCoordinate = Integer.parseInt(answer);
            System.out.print("Enter the y coordinate: ");
            String answer2 = myScanner.nextLine();
            int yCoordinate = Integer.parseInt(answer2);
            for(int i = 0; i < 15; i++)
            {
                for(int j = 0; j < 15; j++)
                {
                    if(xCoordinate == i)
                    {
                        if(yCoordinate == j)
                        {
                            System.out.printf("x", boardList[i][j]);
                        }
                        else
                        {
                            System.out.printf("-", boardList[i][j]);
                        }
                    }
                    else
                    {
                        System.out.printf("-", boardList[i][j]);
                    }

                }
                System.out.println();
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.