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 3 of 3

Thread: Exception in main thread for 2D Array (columns)

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in main thread for 2D Array (columns)

    Hello everyone I am very new to Java programming and need help with this program. I need to print a list of numbers by columns instead of rows i.e. for the table: 10 12 13 14 I need to print 10, 15, 19, 12, 16, etc. instead of 10, 12, 13, 14, etc.
    15 16 17 18
    19 20 21 22

    BUT I keep getting an exception in main thread error. The exact error message after I compile it is: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Assign7.main<Assign7.java:12>

    Yes I am in a class but I'm allowed to ask for help outside of the classroom.

    public class Assign7 {
      public static void main(String args[]) {
        int[][] intar = {
          { 10, 12, 13, 14 },
          { 15, 16, 17, 18 },
          { 19, 20, 21, 22 }
        };
     
           for (int row = 0; row < intar.length; row++)
    	  {
    	     for (int col = 0; col < intar[row].length; col++)
    	        System.out.println(intar[col][row]);
    	  }
      }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception in main thread for 2D Array (columns)

    Look closely at how you have defined your array to represent rows and columns, then look at how you are looping over and then accessing the row and column (in particular look at the very line the exception is being thrown). Write this out on paper if you have to, as this sometimes helps.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in main thread for 2D Array (columns)

    Thanks for the suggestion!

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  2. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  3. exception in thread main java.lang.Nullpointerexception
    By westandeast in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 09:08 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM

Tags for this Thread