Results 1 to 7 of 7
  1. #1
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Array index out of bounds exception

    hello all

    I need some help regarding this matter. I am getting
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at lsb2.main(lsb2.java:44)


    I am unable to understand why I am getting this error, though I feel that conceptually I am right:confused:
    Here is the code
    Java Code:
    import java.io.*;
    import java.lang.Integer.*;
    import java.lang.Math.*;
    import java.util.*;
    class lsb2
    {
       static int m;
       public static int lsb(int i)
       {
         String a=Integer.toBinaryString(i);
         //System.out.println(a);
         String b=a.substring(a.length()-m);
         int lsbval=Integer.parseInt(b,2);
         //System.out.println(b);
         //System.out.println(lsbval);
         return lsbval;
       }
      public static int power(int x, int y)
      {
         int res=1;
         for(int i=0;i<y;i++)
            res=res*x;
          return res;
       }
    
     public static void main(String args[]) 
    {
     int n,l,i,j=0,t1,t2;
    String str,a,b;
    try
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter the no.of lsbs to be selected m:");
    m=Integer.parseInt(br.readLine());
    System.out.println("enter the message to be embedded e(in binary form):");
    str=br.readLine();
    l=str.length();
    n=l/m;
    System.out.println("l: "+l+" m: "+m+" n=l/m: "+n);
    int x[]=new int[n];
    int e[]=new int[n];
    System.out.println("Please enter pixel values:");
    [COLOR="Red"]for(i=0;i<=n;i++)
      x[i]=Integer.parseInt(br.readLine()); //the problem occurs here i guess[/COLOR]
    for(i=1;i<=n;i++)
    {
       e[i]=Integer.parseInt(str.substring(j,j+m));
       j=j+m+1;
    }
    for(i=1;i<=n;i++)
    {
      t1=lsb(x[i-1]);
      t2=lsb(x[i]);
      x[i]= x[i]+e[i]-((t1+t2)%(power(2,m)));
    }
    System.out.println("The modified pixel values are:");
    for(i=0;i<=n;i++)
       System.out.println(x[i]);
    }catch(IOException e)
      {
    	System.out.println("IOERROR");
       }
    
    }
    }
    The sample input is
    m=2
    e=01011001
    pixel values:
    160
    28
    95
    19
    225

    The output for the above input must be
    160
    29
    96
    18
    223


    Any help is appreciated. Please help me out people.
    Thank you :)
    with warm regards,
    Sruthi :)

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Common array error

    I haven't looked over your code too closely,
    but I spotted a very common error that beginners
    make.

    Your error message says it all; you've gone beyond
    the range of the array.

    The fix:

    change

    for(i=0; i<=n; i++)

    to

    for(i=0; i<n; i++)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I am unable to understand why I am getting this error, though I feel that conceptually I am right
    The JVM is rarely wrong. Your array goes from 0 to n - 1. Your for loop goes from 0 to n. The solution is to make your for loop condition to be i < n not i <= n

  4. #4
    sruthi_2009 is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    @ Paul and @ Fubarable

    Thank you so much. By brooding over other issues in my program, I forgot the most basic thing that an array runs from 0 to n-1.
    Feeling so dumb right now:o :o :eek:

    Anyways, thank you for helping me out. :)
    with warm regards,
    Sruthi :)

  5. #5
    Seth_Mclife is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Magic Square Out of bounds

    hi, i have problem here, i dont know how this to resolve, this code process but the out put always like this "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at MagicSquare.main(MagicSquare.java:13)"

  6. #6
    Seth_Mclife is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Magic Square Out of bounds

    hi, i have problem here, i don't know how to resolve this. this code process but the out put always like this "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at MagicSquare.main(MagicSquare.java:13)" i expect that the out put must be a magic square :mad: ,

    this is the code.

    public class MagicSquare {

    public static void main(String[] args) {
    int N = Integer.parseInt(args[1]);
    if (N % 2 == 0) throw new RuntimeException("N must be odd");

    int[][] magic = new int[N][N];

    int row = N-1;
    int col = N/2;
    magic[row][col] = 1;

    for (int i = 2; i <= N*N; i++) {
    if (magic[(row + 1) % N][(col + 1) % N] == 0) {
    row = (row + 1) % N;
    col = (col + 1) % N;
    }
    else {
    row = (row - 1 + N) % N;
    // don't change col
    }
    magic[row][col] = i;
    }

    // print results
    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
    if (magic[i][j] < 10) System.out.print(" "); // for alignment
    if (magic[i][j] < 100) System.out.print(" "); // for alignment
    System.out.print(magic[i][j] + " ");
    }
    System.out.println();
    }

    }


    }

  7. #7
    Anthonynasi is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default My story

    My name is Sasha.
    I 'm writing this letter at the request of my friends( Fubarable )
    I am 23 years
    I work during the day and in the evening at home. Times on dating is not enough.
    All the guys I know are not more than friends . I really cared about how to find a male sex .
    I 'm doing a career and I do not want the relationship .
    On the advice of my friend , I visited this site : фото и видео. Секс знакомства Meendo
    And found what I was looking for
    Yes, I'm ashamed of what I did, but it really helped me with sex
    If it will help someone , I'll be glad .
    Sincerely Sasha

Similar Threads

  1. [SOLVED] Search problem - array out of bounds
    By viper110110 in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 05:26 AM
  2. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 08:21 PM
  3. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 09:16 AM
  4. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 02:20 AM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •