Welcome to Dream.In.Code
Become a Java Expert!

Join 150,422 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,096 people online right now. Registration is fast and FREE... Join Now!




Help with code

 
Reply to this topicStart new topic

Help with code

dwd3773
17 Mar, 2008 - 03:55 PM
Post #1

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
Ok this program is supposed to take number 1-12 and create a 2 dimensional array as follows:
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},

and display the output as 1,5,9,2,6,10,3,7,11,4,8,12.

I need it to display the number in that orde but the program I crated displays them in regular numerical order. Heres my code so far:
CODE

public class Assign8
{
    public static void main(String[] args)
    {
        int [][] inTar =
        {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
        };

        for (int row = 0; row < inTar.length; row++)
        {
            for(int col= 0; col < inTar[row].length; col++)
            {
                System.out.println(inTar[row][col]);
            }
        }
    }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Help With Code
17 Mar, 2008 - 04:10 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Do this inside your for loop:
System.out.println(inTar[col][row] );
Just reverse the row and col variables.
User is online!Profile CardPM
+Quote Post

dwd3773
RE: Help With Code
17 Mar, 2008 - 04:48 PM
Post #3

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
I tried that and everything compiles correctly, but when I run the application it displays the first 3 numbers 1,5,9 correctly, but then I get an error as follows:
exception in thread "main" java.lang.arrayindexoutofboundsexception:3
at assign8.main<assign8.java:17>
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Help With Code
17 Mar, 2008 - 05:05 PM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Then try this:
java

for (int col = 0; col < inTar[0].length; col++)
for (int row = 0; row < inTar.length; row++)
System.out.print(inTar[row][col] );


I just reverse the inner and outer for loops.
User is online!Profile CardPM
+Quote Post

dwd3773
RE: Help With Code
17 Mar, 2008 - 05:19 PM
Post #5

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
Thank you, that worked perfectly. I would have never thought about adding the zero there.
User is offlineProfile CardPM
+Quote Post

Steven Smith
RE: Help With Code
17 Mar, 2008 - 05:38 PM
Post #6

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 25


My Contributions
Your error was in creating an out of bounds reference through inTar[col].length.

Say you have 4 rows and 3 columns. The length of the column is four. The length of column 1 is four, the length of 2 is four, etc... This is your logic. Unfortunately, you asked the program to return the length of column 4... but you only have 3 columns.

Next time, break out your maxRow, and maxCol references outside your for loop. They don't need to be reevaluated every loop, it wastes resources. It also forces you to link about your bounds better ahead of time.

This post has been edited by Steven Smith: 17 Mar, 2008 - 05:39 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month