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 Thread Main ArrayIndexOutOfBoundsException

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

    Default Exception In Thread Main ArrayIndexOutOfBoundsException

    Can someone maybe help me with the following error. Exception in thread main java.lang.arrayindexoutofboundsexception : 3 at RowTrans.encrypt(Rowtrans.java:33)
    at RowTrans.main(Rowtrans.java :7)

    In my program I want to get a text. Put it in a matrix with 5 columns and determine the rows according to the length of the text. Then i want to change the column and row position so that the row gets the columns position and the column the row. Can anyone assist me on this error please.

    Here is my code

    import java.util.Scanner;
    03

    04
    public class ColTrans {
    05
    public static void main(String[] args)
    06
    {
    07
    String ori = "This is my horse";
    08
    String enc = encrypt(ori);
    09
    System.out.println(enc);
    10
    // String dec = decrypt(enc);
    11
    // System.out.println(dec);
    12
    }
    13

    14
    static String encrypt(String text)
    15
    {
    16
    String result = "";
    17
    text = text.toUpperCase();
    18
    int length = text.length();
    19
    int rows = length/5;
    20
    char [][] b =new char[rows][5];
    21
    char [][] c =new char[5][rows];
    22
    char [] d = new char[length];
    23
    if ((length % 5) != 0)
    24
    rows = rows + 1;
    25
    int k = 0;
    26
    for(int i = 0; i < rows; i++)
    27
    for(int j = 0; j < 5; j++)
    28
    {
    29
    if ( k > length )
    30
    b[i][j] = 'Z';
    31
    else
    32
    {
    33
    d[k] = text.charAt(k);
    34
    b[i][j] = d[k];
    35
    }
    36
    k++;
    37
    }
    38
    for(int i = 0; i < 5; i++)
    39
    for(int j = 0; j < rows; j++)
    40
    {
    41
    c[i][j] = b[j][i];
    42
    result = result + c[i][j];
    43
    }
    44
    return result;
    45
    }
    46
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Exception In Thread Main ArrayIndexOutOfBoundsException

    repost your code with code tags, that is unredable.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Exception In Thread Main ArrayIndexOutOfBoundsException

    java.lang.arrayindexoutofboundsexception

    Somewhere the program is trying to access an element of an array that is out of bounds.
    Look at the error message for a hint to the line number and figure out why the code is trying to get elements that do not exist.
    (Either less than zero or larger than the last index)

Similar Threads

  1. Exception in main thread for 2D Array (columns)
    By java_begginer in forum Exceptions
    Replies: 2
    Last Post: December 3rd, 2012, 04:44 PM
  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