How to update a 2D array in JOptionPane?

294 Views Asked by At

Yesterday I posted a question asking how to display a 2d array in JOptionPane. I did not really understand the code and I cant seem to update the table no matter what I do. I even tried without JOptionPane and it worked, I'm just so stumped as to what the problem is. All I want is for the cell in the array to change to an 'O' and for some reason it does'nt want to happen.

Here is my code (my indentation is a bit screwed thanks to stack overflow xD) :

import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
public class StarWars {

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    sb.append("<html>");
    int column;
    String [][] gridPlayer = new String [6][6];
    Scanner kb = new Scanner (System.in);

    String [] rows = {"A", "B", "C", "D", "E", "F", "G", "H"};

    for (int i = 0; i < 6; i++){
        for (int j = 0; j < 6; j++){

            gridPlayer[i][j] = " ~ ";
        }
    }


    sb.append("&nbsp&nbsp");
    for (int i = 0; i < 6; i++){
        sb.append(" " + rows[i] + " ");
    }
    sb.append("<br>");
    for (int i = 0; i < 6; i++){
        sb.append(" " + i);
        for (int j = 0; j < 6; j++){
            sb.append(gridPlayer[i][j]);
        }
        sb.append("<br>");
    }
    sb.append("</html>");
    String answer = JOptionPane.showInputDialog(null, new JLabel(sb.toString()));

    int rownum = 0;
    String letter = answer.toUpperCase().substring(0,1);

    for (int i = 0; i < rows.length; i++){

        if (rows[i].equals(letter)){
            rownum = i;

        }
    }

    boolean gridUpdate = true;
    while (gridUpdate == true){

        String answer2 = "";
        answer2 = answer.substring(1,answer.length());
        column = Integer.parseInt(answer2);
        gridPlayer[rownum][column] = " O ";
        gridPlayer[3][4] = " D ";
        gridPlayer[rownum][column] = answer;

        if (gridPlayer[rownum][column].equals(" ~ ")){
            gridPlayer[rownum][column] = " O ";
            gridPlayer[3][4] = " D ";
            gridUpdate = true;


        }

    JOptionPane.showInputDialog(null, new JLabel(sb.toString()));


    }



    }
}
0

There are 0 best solutions below