Results 1 to 7 of 7
Thread: 2d array help
- 01-30-2012, 02:43 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
[Solved] 2d array help
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
Java 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 = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { board[rows][columns] = false; } } for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if(board[rows][columns] == false) { System.out.print("-"); } } System.out.println(); } } }Last edited by nweid1; 01-30-2012 at 03:42 AM.
- 01-30-2012, 03:22 AM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: 2d array help
Why i and j are initialized to 1?
I don't see any random value in code above.Java Code:for (int i = 1; i < rows; i++) { for (int j = 1; j < columns; j++) {Last edited by diamonddragon; 01-30-2012 at 03:25 AM.
- 01-30-2012, 03:31 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 14
- Rep Power
- 0
Re: 2d array help
Good call. I switched them to 0 but it still gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
- 01-30-2012, 03:48 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: 2d array help
How do You access element of an array?
- 01-30-2012, 04:18 AM #5
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Re: [Solved] 2d array help
Firstly it is not always possible that the user is going to input same number of rows and columns i.e. it can be a irregular 2-dimensional array, therefore
you cannot write your inner for-loop the way you have written. More better way is
Secondly as said previously by dragon you are accessing your array elements incorrectly inside the for loop, it has to beJava Code:for(int i=0; i< rows; i++) { for(int j=0; j<board[i].length; j++) { //code } }
instead ofJava Code:board[i][j]
Java Code:board[rows][columns]
- 01-30-2012, 04:36 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 01-30-2012, 05:23 AM #7
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Re: [Solved] 2d array help
Similar Threads
-
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks