Results 1 to 6 of 6
  1. #1
    Peanuts1 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Question need help with two dimensional array and setting it up using user input

    I have been working on this for two and a half weeks now and still haven't figured it out. It is due 11:59pm EST on Sunday and was wondering if I could get a little help. I have to create a two dimensional array that will hold student id (an integer) and grade (an integer). Setup the size of the array according to the size requested by the user. (prompt the user to enter the number of students, then build your array). Once the array is built, prompt the user to enter a student id and grade for each student. Store these in the array.

    Create a separate method within this program. Name the method "averageThis". Pass the array to the method. Within the method calculate the average of all the grades. Let the array return this average. In your main method display the average returned from the method.

    Here is my written out program:

    import java.util.Scanner;

    public class ClassAverage1
    {
    public static void main(String[] args)
    {
    int students; //The number of students
    int Grad = 0;
    int ID = 0;
    int average;


    **Scanner object**
    //Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    //Get the number of students.
    System.out.print("How many students are in your class? ");
    students = keyboard.nextInt();


    //Create an array to hold that number of students.
    int scores[][] = new int [students][ID];

    **code to get student id and grade**
    for ( int id = 0; id <scores.length; id++)
    {
    for (int grad = 0; grad < Grad; grad++);
    {
    System.out.print("Enter student's ID ");
    ID = keyboard.nextInt();
    System.out.print("Enter Student's grades ");
    scores[id][Grad] = keyboard.nextInt();

    }
    }
    }
    }

    That is all I have so far because I can't get past this part. If I could get help with this I could get the rest. Here is the error message I have been getting:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at ClassAverage1.main(ClassAverage1.java:29)

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    import java.util.Scanner;
    
    public class ClassAverage1
    {
    public static void main(String[] args)
    {
    int students; //The number of students
    int Grad = 0;
    int ID = 0;
    int average;
    
    
    //**Scanner object**
    //Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);
    
    //Get the number of students.
    System.out.print("How many students are in your class? ");
    students = keyboard.nextInt();
    
    //Create an array to hold that number of students.
    int scores[][] = new int [students][2]; //we hold id and grade for each //student so number of cols=2
    
    //**code to get student id and grade**
    for ( int id = 0; id <scores.length; id++)
    {
    //for (int grad = 0; grad < Grad; grad++);
    //{
    System.out.print("Enter student's ID ");
    ID = keyboard.nextInt();
    System.out.print("Enter Student's grades ");
    scores[id][0]=ID;
    scores[id][1] = keyboard.nextInt();
    //}
    }
    }
    }

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Becuase ID = 0 it looks like you are trying to make an array of scores[student][0]; and this will give you an error if you try to access because it is an array of empty arrays.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    What's the value of ID when you create your array?

    kind regards,

    Jos

    edit: too slow again; I'm an old sod ... ;-)

  5. #5
    Peanuts1 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Question need help with two dimensional array and setting it up using user input

    The ID is suppose to be any number that the user puts in and then it is stored in the array. The Grad int is suppose to allow for the user to input several values and later summed up and averaged out. After all that is completed the ID and the average is suppose to be displayed.

  6. #6
    Peanuts1 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks all I could not have finished this without your help. Thanks a million

Similar Threads

  1. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 05:21 AM
  2. Problem - input in two-dimensional array
    By PVL268 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 04:58 AM
  3. Mulit dimensional Array
    By jonsamwell in forum New To Java
    Replies: 7
    Last Post: 02-03-2009, 12:35 PM
  4. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM
  5. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM

Tags for this Thread

Posting Permissions

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