Results 1 to 4 of 4

Thread: Help with two dimensional array java

  1. #1
    Join Date
    Nov 2009
    Posts
    518

    Help with two dimensional array java

    Hi
    I am trying to create an two dimensional array that will store student id and grade. The program has to set the size of the array according to the user input. Then the array should be built and finally the user should be requested to add the information for the student and the grade. All this will be stored in the array. I am not sure to figure out what is wrong here , below is my program.
    Code:
    public class Classavg1
    {
    public static void main(String[] args)
    {
    int stu; //The number of stu
    int grd = 0;
    int ID = 0;
    int avg;
    
    **Scanner object**
    //Create a Scanner object for keybrd input.
    Scanner keybrd = new Scanner(System.in);
    
    //Get the number of stu.
    System.out.print("How many stu are in your class? ");
    stu = keybrd.nextInt();
    
    //Create an array to hold that number of stu.
    int scr[][] = new int [stu][ID];
    
    **code to get student id and grde**
    for ( int id = 0; id <scr.length; id++)
    {
    for (int grd = 0; grd < grd; grd++);
    {
    System.out.print("Enter student's ID ");
    ID = keybrd.nextInt();
    System.out.print("Enter Student's grdes ");
    scr[id][grd] = keybrd.nextInt();
    }
    }
    }
    }
    Last edited by GlassFish; 30-11-2009 at 01:58 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Help with two dimensional array java

    Hi
    I read your program and I have done some modification to it, just check it out
    Code:
    public class Classavg1
    {
    public static void main(String[] args)
    {
    int std; //The number of std
    int grd = 0;
    int ID = 0;
    int avg;
    Scanner keybrd = new Scanner(System.in);
    System.out.print("How many std are in your class? ");
    std = keybrd.nextInt();
    int scr[][] = new int [std][2]; //we hold id and grde for each //student so number of cols=2
    for ( int id = 0; id <scr.length; id++)
    {
    //for (int grd = 0; grd < grd; grd++);
    //{
    System.out.print("Enter student's ID ");
    ID = keybrd.nextInt();
    System.out.print("Enter Student's grdes ");
    scr[id][0]=ID;
    scr[id][1] = keybrd.nextInt();
    //}
    }
    }
    }

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Help with two dimensional array java

    Hi
    hope you have tried the above posted program, it should work with you. Your mistakes were, ID=0 it means you are trying to make an array of
    src[std][0],
    int scr[][] = new int [stu][ID];
    **code to get student id and grde**
    for ( int id = 0; id <scr.length; id++)
    {
    for (int grd = 0; grd < grd; grd++);
    {
    System.out.print("Enter student's ID ");
    ID = keybrd.nextInt();
    System.out.print("Enter Student's grdes ");
    scr[id][grd] = keybrd.nextInt();
    }
    }
    and this will definitely give you any error if you try to acces it because it is any array of empty array.

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: Help with two dimensional array java

    Hello
    Thanks a lot for your help guys. I have successfully solved this program and got the required output. Really it would not have been possible to solve the problem without you guys. Thanks a lot again.

Similar Threads

  1. Array of objects in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 02:05 AM
  2. Two dimensional array to a function
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 27-11-2009, 12:47 PM
  3. Dynamically allocate a two-dimensional array
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 24-10-2009, 11:43 AM
  4. Three Dimensional Array program in Java.
    By nonose in forum Software Development
    Replies: 3
    Last Post: 10-08-2009, 07:49 PM
  5. JAVA array
    By Daren in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,889,699.34320 seconds with 17 queries