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: Why isn't this 2D array working?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why isn't this 2D array working?

    Given a 2D array of Strings and a non-empty array of Strings where each string is of length 2 or more. Write a method that will place the first 2 chars of each String into a 2D array in row major order. If the 1D array runs out of strings then fill in the rest of the elements with $$.

     
        public static void main(String[] args)
        {
     
            String[][] t = new String[2][3];
            String[][] test;
            String[] words = {"hello", "blah", "boom", "elephant"};
            test = twoCharsTo2D(t, words);
            print(test);
     
     
        }
     
        public static String[][] twoCharsTo2D(String[][] table, String[] words)
        {
            for(int r = 0; r<table.length; r++)
            {
                for(int c = 0; c<table[r].length; c++)
                {
                    table[r][c] = "$$";
                }
            }
            int counter = 0;
            for(int rows = 0; rows<table.length; rows++)
            {
                if(counter==words.length)
                    break;
                for(int col = 0; col<table[rows].length; col++)
                {
     
                    table[rows][col] = words[col].substring(0,3);
                    counter++;
                }
            }
            return table;
     
        }

    It's a simple problem, but when I try printing this method I get

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

    Thanks!


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Why isn't this 2D array working?

    i think the problem with print(test) method. Please post the print(String[][]) method.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Why isn't this 2D array working?

    ava.lang.ArrayIndexOutOfBoundsException: 2
    Please post the full text of the error message that shows where the error happens.
    The error says that there is an index past the end of the array.
    Remember that the indexes for an array range from 0 to the length-1
    The max index for an array with 2 slots is 1.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] I can't figure out why this isn't working!
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 03:04 PM
  2. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 19th, 2012, 07:00 PM
  3. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM
  4. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 03:08 PM
  5. newLine isn't working
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 8th, 2011, 10:42 AM