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: Matrix Program

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

    Default Matrix Program

    I was writing a simple Java program to input a matrix and then display it using 2-dimensional arrays. I used the same logic which I had previously used for the same program in C. But it isn't working. It goes on taking taking values until the index goes out of bound and then displays an error :

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at apples.main(apples.java:136)"

    The line 136 is: a[i][j] = mad.nextInt(); in bold below


    import java.util.Scanner;

    class apples{
    public static void main(String args[]){
    int a[][] = new int[4][4];
    int m,n;
    Scanner mad = new Scanner(System.in);

    System.out.println("Enter the order");
    m = mad.nextInt();
    n = mad.nextInt();
    System.out.println("Order is " + m + "x" + n);
    System.out.println("Enetr the elements");
    for(int i=0;i<m;i++){
    for(int j=0;i<n;j++){
    a[i][j] = mad.nextInt();
    }
    }
    System.out.println("The matrix is");
    for(int i=0;i<m;i++){
    for(int j=0;i<n;j++){
    System.out.println(a[i][j]);
    System.out.println("\t");
    }
    System.out.println();
    }

    }
    }
    Are there other methods of doing this. The user has to give the input for the matrix and then it should be displayed.


    Thank you,


  2. #2
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Matrix Program

    hmmmm......

    how about try changing your for loops:

    for(int i=0;i<4;i++){
    for(int j=0;i<4;j++){
    a[i][j] = mad.nextInt();
    }
    }

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

    Default Re: Matrix Program

    Few edits are there to run the program correctly------------


    import java.util.Scanner;

    class apple{
    public static void main(String args[]){
    int a[][] = new int[4][4];
    int m,n;
    Scanner mad = new Scanner(System.in);

    System.out.println("Enter the order");
    m = mad.nextInt();
    n = mad.nextInt();
    System.out.println("Order is " + m + "x" + n);
    System.out.println("Enetr the elements");
    for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
    a[i][j] = mad.nextInt();
    }
    }
    System.out.println("The matrix is");
    for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
    System.out.print(a[i][j]);
    }
    System.out.println();
    }

    }
    }



    the major mistake is in the loops!!

Similar Threads

  1. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 02:41 PM
  2. Issue Building Graph in Adjacency Matrix
    By mike.s in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 22nd, 2010, 03:44 PM
  3. Embedding images in a matrix.
    By Aims_ in forum Java Theory & Questions
    Replies: 1
    Last Post: September 11th, 2009, 03:37 PM
  4. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 05:40 PM