0

so im having problems polishing up my program. this program is supposed to create a 1D array with a user input. then it creates a box of 'O's like this..

N = 4

OOOO
OOOO
OOOO
OOOO

the user inputs coordinates based on the box and the 'O' is changed to an 'X'. the program is supposed to repeat itself after the coordinates are selected while remembering the position of X and including it in the next loop.

i tried implementing a while loop but it seems that code just loops over the Array without remembering the last position of X.

how could i change the code so it does what i need it to do?

public static void makeArray(int M) {
    String input = "";
    boolean repeat = false;
    int N = InputNumber(input);
    String[] Board = new String[N];
    M = (int) Math.sqrt(N);
    String A = "O";
    String B = "X";

    System.out.println("Printing " + (M) + " x " + (M) + " board...");
    System.out.println("Done.");
    System.out.println();

    while (!repeat) {
        int X = Xvalue(M);
        int Y = Yvalue(M);

        int C = convertIndex(X, Y, M);

        System.out.println("Marking location " + X + "," + Y + ")");
        for (int i = 0; i < (Board.length); i++) {
            {
                Board[i] = A;
                if ((i % M == 0)) {
                    System.out.println();
                }
                if (i == C) {
                    Board[i] = Board[i].replace(A, B);
                }
                if (i == C && C == -1) {
                    repeat = true;
                }
            }
            System.out.print(Board[i]);
        }
        System.out.println();
    }
}

public static int convertIndex(int x, int y, int N) {

    int valX = (x - 1) * N;
    int valY = y;
    int targetIndex = valX + valY;

    return (targetIndex - 1);

}

public static int Xvalue(int M) {
    boolean repeat = false;
    int X = 0;
    while (!repeat) {

        System.out.print("Please enter the X-coordinate: ");
        String InputX = new Scanner(System.in).nextLine();
        X = Integer.parseInt(InputX);
        if (X > M) {
            System.out.println();
            System.out.println("Error, please enter a valid X Coordinate...");
            repeat = false;
        } else {
            repeat = true;
        }
    }
    return X;
}

public static int Yvalue(int M) {
    boolean repeat = false;
    int Y = 0;

    while (!repeat) {
        System.out.println();
        System.out.print("Please enter the Y-coordinate: ");
        String InputY = new Scanner(System.in).nextLine();
        Y = Integer.parseInt(InputY);
        if (Y > M) {
            System.out.println("Error, please enter a valid Y Coordinate...");
            repeat = false;
        } else {
            repeat = true;
        }
    }
    return Y;
}
5
  • 1
    Have you tried storing it in another variable? (this seriously looks like homework) Commented Nov 27, 2015 at 17:57
  • @Joe so what if its hw? i did not ask you to write the program for me. also what do you mean by storing it in another variable? Commented Nov 27, 2015 at 18:06
  • 1
    The problem is not so much that it is homework, The problem is that you have not provided a minimal example, instead burdened the us with your entire program. This question is unlikely to ever be useful to anyone else. Commented Nov 27, 2015 at 18:15
  • @bhspencer i see, was just trying to be complete, will try a shorter question next time. Commented Nov 27, 2015 at 18:19
  • 1
    Also, it's confusing that your "N=4" example does not match with specifying N=4; in code but with N=16;. Similarly that makeArray(int M) accepts M but reassigns it without ever using it's value. Being concise about these kind of things can make your intent much clearer, and will help to avoid semantic issues when you write more complicate programs. Commented Nov 27, 2015 at 18:25

2 Answers 2

1

The trouble with you loop is that it defines every element in you your array before it prints them:

while (!repeat) {
    //...
    for (int i = 0; i < (Board.length); i++) {
        {
            Board[i] = A; //Makes each element "O"
            //...
            if (i == C) { //Makes only the current cooridinate "X"
                Board[i] = Board[i].replace(A, B);
            }
            //...
        }
        System.out.print(Board[i]);
    }
}

To fix it so that old X's are retained, you need to remove assignment Board[i] = A;. But you'll still need to initialize your board, or else you'll have null strings. So you need to add something before the loop like:

String[] Board = new String[N];
M = (int) Math.sqrt(N);
String A = "O";
String B = "X";
//initialize board
for (int i = 0; i < Board.length; i++)
    Board[i] = A;
Sign up to request clarification or add additional context in comments.

2 Comments

i see what your saying here but i thought that String[] Board = new String[] initializes the board?
So to initialize means to "set to the value [...] appropriate to the start of an operation." When Java initializes a field it refers to a specific convention of providing default values such as 0 or null. But null strings aren't appropriate for your loop. Java just uses the word initialize to describe it's own particular convention, it's not the only sort there is.
0

Try using a char[][] instead of a String[]. Then you can just plugin the coordinates the user inputs (e.g. board[x][y] = B). This better represents what you're showing the user as well.

This saves you from having to loop through your String[] and then finding the right character to change. Remember, Strings are immutable, so you'd have to reassign the entire string after replacing the right character. With the char[][] you simply assign 'X' to the right coordinates.

EDIT:

Since a single array is required, you should be able to do the following (instead of looping):

board[x] = board[x].substring(0, y) + A + board[x].substring(y + 1);

1 Comment

the purpose of this assignment is to actually use a 1D array forgot to mention i am not allowed using a 2D array.

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.