Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Printing 2d array

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Solved] Printing 2d array

    I have to write a program for my programming class prints out a table filled with "-"s and randomly fills them with "#"s. So first, I have to create a 2d array where the number of rows and columns are given by the user. Then, I have to print the initial table with all "-"s. So, I created a boolean array with the number of rows and columns as the user input, and make a for loop to set every spot in the array as false. Then I may a loop that says if the spot is false, print out a "-". If I enter a number over 1 for the column or row, it won't work. If I put 1, it runs but doesn't print out. Please help! What am I doing wrong? A sample of the output would be:

    Rows: 5
    Columns: 8

    - - - - - - - -
    - - - - - - - -
    - - - - - - - -
    - - - - - - - -
    - - - - - - - -

    Heres the code

    public class Matrix2 {
     
    import java.util.Random;
    import java.util.Scanner;
     
     
    	public static void main(String[] args) {
     
    		Scanner console = new Scanner(System.in);
    		System.out.println("Number of rows?");
    		int rows = console.nextInt();
     
    		System.out.println("Number of columns?");
    		int columns = console.nextInt();
     
     
    		boolean board[][] = new boolean[rows][columns];
     
    		for (int i = 1; i < rows; i++) {
    			for (int j = 1; j < columns; j++) {
     
    				board[rows][columns] = false;
     
    			}
    		}
     
    		for (int i = 1; i < rows; i++) {
    			for (int j = 1; j < columns; j++) {
     
    				if(board[rows][columns] == false) {
    					System.out.print("-");
     
    				}
     
    			}
    			System.out.println();
    		}
    	}
    }
    Last edited by nWeid1; January 29th, 2012 at 09:42 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing 2d array

    I think you need to think about what you want your loops to do. You want something to change every time you go iterate through the loop, but the way you have it set up right now, nothing does. Also, you want to make sure you initialize your loop pointers at 0 instead of 1.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing 2d array

    Well, the first loop can be deleted for now. At this point in time, I just want to print out a table of -'s of a given number of rows and columns and isn't that what the second loop does? It starts at row 1 and increases by 1 until it hits the row the user gave which is given by the variable rows. Then it goes through every column and prints out a -.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Printing 2d array

    Remember that you should not use an equality test for pbooleans, but rather use the built-in expression.

    For example, if you were checking if board[rows][columns] was false, you should use this :
    if(!board[rows][columns])
    {
     
    }

    However, another thing you want to note is that you don't want to use
    boards[rows][columns]
    in your loop, but rather use your loop counters, EG
    boards[i][j]

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing 2d array

    Tjstretch, I am an idiot for using board[rows][columns] and not boards[i][j] lol thanks for pointing out the obvious. And thanks for the !board[][] tip. It's working now, thanks!

Similar Threads

  1. Printing out numbers from an Array.
    By djl1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 13th, 2011, 12:03 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 09:07 AM
  4. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM