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: Array exception out of bounds

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Array exception out of bounds

    import java.util.Scanner;
    public class masyvai
    {
    public void masyvas()
    {
    //sk – 5 , z – 2,4,6,8
    Scanner scan2 = new Scanner(System.in);
    int a,b;
     
    System.out.printf("Iveskite eiluciu kieki");
    a=scan2.nextInt();
    System.out.printf("Iveskite stulpeliu kieki");
    b=scan2.nextInt();
    int mtr[][] = new int[a][b];
     
     
    System.out.println(" --------------------------------------------------");
    System.out.println("| Sumavimas | Atimtis |");
    System.out.println(" --------------------------------------------------");
     
    for (int eil = 0; eil < a; eil++)
    {
    for (int stul = 0; eil < b; stul++)
    {
     
    System.out.print(" "+mtr[eil][stul]); // error here, wtf???
    }
    System.out.print("\n");
     
    }
    scan2.close();
     
    }
    }

    What can be the problem here? I really dont understand, I used this code on my netbook (XP win) and it works fine but when i used it on my pc (win7) it crashes and gives me a gift:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at masyvai.masyvas(masyvai.java:28)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Array exception out of bounds

    Why are you comparing eil to both a and b in your inner and outer loop? Is that really what you want to be doing in your inner loop?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Array exception out of bounds

    Hello gabberlt. Welcome to the Java Programming Forums.

    The java.lang.ArrayIndexOutOfBoundsException means that your Array Index is Out of Bounds

    I would look at the for loops. Try using System.out.println(); to see what's going on.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 06:11 AM
  2. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 08:14 AM
  3. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  4. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 11:19 AM
  5. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 01:47 PM