0

My print method is not printing out all of the columns. It is only printing out half of the columns, but it is printing out all of the rows.

How do I change my print method so that it prints out all of the columns?

import java.io.*; import java.util.Scanner; public class T_GameOfLife { private char[][] board; private int columns; private int rows; private int generation; public static void main(String[] args) throws FileNotFoundException { T_GameOfLife game = new T_GameOfLife(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter how many generations to compute: "); int gen = keyboard.nextInt(); System.out.println("Generation 1:"); game.print(); for (int i = 2; i <= gen; i++) { System.out.println("Generation " + i); game.computeNextGeneration(gen); game.printNew(); } } public T_GameOfLife() throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); System.out.print("Enter file name: "); String filename = keyboard.nextLine(); File file = new File(filename); Scanner inputFile = new Scanner(file); int columns = inputFile.nextInt(); int rows = inputFile.nextInt(); inputFile.nextLine(); board = new char[rows][columns]; for (int i = 0; i < rows; i++) { String line = inputFile.nextLine(); for (int j = 0; j < columns; j++) { board[i][j] = line.charAt(j); } } } public int getColumns() { return columns; } public int getRows() { return rows; } public int getCell(int rows, int columns) { if (board[rows][columns] == 'X' || board[rows][columns] == '0') { return board[rows][columns]; } else { return 0; } } public void setCell(int rows, int columns, int value) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { value = board[rows][columns]; } } } public void print() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print( board[i][j] ); } System.out.println(); } } } 
0

2 Answers 2

0

Your setCell function seems to be doing nothing. I think you meant to write:

board[rows][columns] = value; 

instead of what you currently have.

As far as the printing part, your function is printing out all the rows and columns, so probably the board is half filled with empty or unprintable characters.

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

6 Comments

If I write it like that, then it doesn't work. the following works: value = board[rows][columns]; Does it do the same thing though?
@Komal it doesn't do the same thing. One is reading a value in the array and giving it to a variable, the other is giving the value of the variable to a cell in the array.
As @JS1 had mentioned, that function seems to do nothing.
Removing the loops in the setCell function won't make any change to the program, as none of the parameters in it are used.
@ McAdam331 I write the statement as board[rows][columns] = value; but I get a possible loss of precision error. How would I fix that?
|
0

Based on the funny conversation you had in the other solution, you might need to look at an introduction to programming course at edx or coursera before you continue this java programming adventure...

wrt to your questions though..

board[rows][columns] = value; 

is what you want, otherwise setcell does nothing. And the error for precision is referring to the use of char for your board while your value is declared as int, ie type error.

For the print loop, i can't deduce anything, but you might want to try using 'rows' and 'columns' instead of .length

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.