i need help trying to set a program that will have three arrays one for number of students anothe rfor 3 exams and final the average of all the grades can any one help

Recommended Answers

So show your code so far.

Jump to Post

I'm just stuck on how to set it up and my professor is no help

heh heh, how many times have I heard that?!

Jump to Post

All 7 Replies

Member Avatar

So show your code so far.

import java.util.*; 
public class array
{
    public static void main (String [] args)
    {
        int s, e, sum;
        double avg;
        Scanner input = new Scanner(System.in);
        String [] exam = {"exam1", "exam2", "exam3"};
        e= exam.length;
        int[]exam1= new int [e];
        int[]exam2= new int[e];
        int[]exam3= new int[e];
        System.out.println("enter a grade for exam 1:" +exam[e]);
        exam1[e]=input.nextInt();
        System.out.println("enter a grade for exam 2:" +exam[e]);
        exam2[e]=input.nextInt();
        System.out.println("enter a grade for exam 3:" +exam[e]);
        exam3[e]=input.nextInt(); 
        }

What do you need help with at that point?

I mean I'm still new to java .. Like the assignment was to set up separate array for students , their exams and there grade/avg .. I'm just stuck on how to set it up and my professor is no help

 int[]exam1= new int [e];
int[]exam2= new int[e];
int[]exam3= new int[e];
System.out.println("enter a grade for exam 1:" +exam[e]);
exam1[e]=input.nextInt();
System.out.println("enter a grade for exam 2:" +exam[e]);
exam2[e]=input.nextInt();
System.out.println("enter a grade for exam 3:" +exam[e]);
exam3[e]=input.nextInt(); 

this part of your code makes no sense.

  1. you only need one array, not three of them
  2. exam1[e]=input.nextInt(); not only does it make no sense in doing this three times, but it's a runtime exception (arrayIndexOutOfBoundsException) waiting to happen.
    the length of your array is e. so, that means the valid indices go from 0 to e-1.
    element with index e doesn't exist, because it is 'out of bounds'.
    even if it was a valid index, you set three times the element with the same index, so you would just be overwriting the value
  3. System.out.println("enter a grade for exam 1:" +exam[e]);
    same here: exam[e] doesn't exist. it's out of bounds.
    but, since you are already printing exam [number], why would you print that exam-element?

you should have something like:

int[] exams = new int[e];
for ( int i = 0; i < e; i++){
  System.out.println("enter a grade for exam " + (i+1) + ": " );
  exams[i]=input.nextInt(); 
  }

here, each iteration the value of i is updated, so it won't overwrite data you've entered. and, because of the i < e statement, the iteration will end before an ArrayIndexOutOfBoundsException can occur.

And if you want to print the array afterwards, you could do the for loop but instead of writing to exams[i], you just print out the value exams[i]

Member Avatar

I'm just stuck on how to set it up and my professor is no help

heh heh, how many times have I heard that?!

commented: lolz +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.