my program needs to print a letter in a specific row and column after asking the questions for which row and column. It will print the letter in the right column but it will only print on the 1st row and the the second or third if i select the third row. I have a seperate cell and driver class but the problem is coming from this class.... Here is the class and what it looks like when it prints out...
If you can help I would greatly appreciate it. Thanks.
import java.util.*;
public class CellInterface {
private int option;
private int columns;
private int rows;
private int cellRow;
private int cellColumn;
private Cell[][] cells;
private Scanner sc = new Scanner(System.in);
public CellInterface(){
System.out.println("How many rows would you like? ");
this.rows = sc.nextInt();
System.out.println("How many columns would you like? ");
this.columns = sc.nextInt();
this.option = 1;
this.cellRow = 0;
this.cellColumn = 0;
this.cells = new Cell[rows][columns];
for(int r = 0; r < this.cells.length; r++){
for(int c = 0; c < this.cells[r].length; c++){
this.cells[r][c] = new Cell(' ');
}
}
this.cellModifier();
}
public void cellModifier(){
while(option == 1){
System.out.println("Enter a row: ");
this.cellRow = sc.nextInt();
System.out.println("Enter a column: ");
this.cellColumn = sc.nextInt();
System.out.println("Enter a letter: ");
this.cells[cellRow][cellColumn].setLetter(sc.next().charAt(0));
this.printGrid();
System.out.println("Press 1 to modify: Press 2 to exit: ");
this.option = sc.nextInt();
}
}
public void printGrid(){
for(int r = 0; r < this.cells.length; r++){
for(int c = 0; c < this.cells[r].length; c++){
System.out.print(this.cells[r][c].getLetter() + '\n');
}
}
}
}
this is what it looks like when its printed:
How many rows would you like?
5
How many columns would you like?
5
Enter a row:
2
Enter a column:
4
Enter a letter:
A
42424242424242424242424242427542424242424242424242
Press 1 to modify: Press 2 to exit: