-3

I am working on a program that has to check for certain conditions (and give out an error message if they are not executed) before executing two for loops (one for the sum of grades entered and one for highest and lowest grades)and I'm not sure how to sequence them.

Also my code is getting me this error -->Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 and I'm not sure what to do to fix the error

If anyone can help me with the skeletons of the correct sequence I would truly appreciate it!

Here is my code (I know it looks crazy, sorry I'm learning!):

import java.util.Scanner;

public class Homework {

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        int numberOfMarks, average, sum, total, mark;
        numberOfMarks = 0;
        int [] marks = new int[numberOfMarks];
        int smallest, largest;
        //System.out.println("Enter number of marks: ");
        numberOfMarks = input.nextInt();
        if(numberOfMarks<0) {
        System.out.println("Number of marks must be greater than 0!!");
        }
        //System.out.println("Enter "+numberOfMarks+"marks: ");
        mark = input.nextInt();
        if(mark<0) {
            System.out.println("Negative marks not allowed!!!");
        } else if(mark>100) {
            System.out.println("Marks above 100% not allowed!!!");
        }  
        sum = 0;
        for(int i = 0; i < marks.length; i++) {
            sum += marks[i];
        }
        smallest=marks[0];
        largest=marks[0];
        for(int i=1;i<marks.length;i++) {       
            if(marks[i]>largest) {
                largest=marks[i];
            } else if(marks[i]<smallest) {
                smallest=marks[i];
            }
            average = sum/numberOfMarks;
            System.out.println("Highest Mark = "+largest);
            System.out.println("Lowest Mark = "+smallest);
            System.out.println("Average = "+average);
        }
    }
}
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
August4
  • 7
  • 3
  • You should indent your code properly before asking people to try and read it. – khelwood Apr 19 '18 at 09:46
  • 1. format your code properly. 2. [mcve] 3. one problem at the time, otherwise it's too broad. 4. https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Jaroslaw Pawlak Apr 19 '18 at 09:47
  • 1
    Your array has a size of `0` , `marks[0]` attempts to access the first entry, so it crashes . – Arnaud Apr 19 '18 at 09:47
  • 2
    `numberOfMarks = 0; int [] marks = new int[numberOfMarks];` will create an **empty** array. You have to get user input before you create it – QBrute Apr 19 '18 at 09:47
  • `int [] marks = new int[numberOfMarks];` <- numberOfMarks is 0 at this point, so you initialize your marks array with a size of 0. It doesn't matter if the value of numberOfMarks changes later because the intialization of an array happens only once. – OH GOD SPIDERS Apr 19 '18 at 09:47
  • Thanks for the responses! I'm sorry about the formatting, I indented it as I was prompted to do, not sure how else to format it? – August4 Apr 19 '18 at 10:02

2 Answers2

0

Problem is in the logic, see comments bellow

import java.util.Scanner;

public class Homework {

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);

  int numberOfMarks, average, sum, total, mark;
  numberOfMarks = 0;
  /*there you are creating empty array
    you told marks should have length of value numberOfMarks, but 
    numberOfMarks is equal to zero at this time
  */
  int[] marks = new int[numberOfMarks]; 
  int smallest, largest;

  //System.out.println("Enter number of marks: ");
  numberOfMarks = input.nextInt();

 /*
   not interesting conditions checking... 
 */

  for (int i = 0; i < marks.length; i++) {
   /*
     There you are trying to access 1st element of marks
       - indexing from zero, as usual
     But there is no first element, array has zero length, zero elements
      Ok, you told array should have length of numberOfMarks, but it was 
       when value was zero, so if you will change numberOfMarks later, it 
        has no impact on the length of array
   */
   sum += marks[i];
  }

  /*
    stats computing, not important
  */

 }

}

So, once you got user input for number of marks, then you can "recreate" - realloc / redefine the array, as before marks = new int[numberOfMarks];

In your case bellow

//System.out.println("Enter number of marks: ");
numberOfMarks = input.nextInt();

Using variable in length of array does not means it will be bounded, so if variable in array length (in this case numberOfMarks) will change later after usage, then length of array will be still the same

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
0

You can try this:

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int numberOfSubjects, sum;
        numberOfSubjects = 0;

        System.out.print("Enter the total number of subjects: ");
        numberOfSubjects = in.nextInt();

        if(numberOfSubjects < 0){
        System.out.println("Number of marks must be greater than 0!!");
        }
        else {
            int []marks = new int[numberOfSubjects];
            sum = 0;

            System.out.print("Enter the marks: ");
            // add condition inside for loop for marks not less than 1 or greater than 100 
            for(int i=0; i<marks.length; i++) {
                    marks[i] = in.nextInt();
            }

            // calculate total
            for(int i = 0; i < marks.length; i++){
                sum += marks[i];
            }

            // sort marks
            Arrays.sort(marks);

            System.out.println("Total: " + sum);
            System.out.println("Average: " + sum/numberOfSubjects);
            System.out.println("Minimum: " + marks[0]);
            System.out.println("Maximum: " + marks[marks.length-1]);
        }
    }
}

Code flow should look like this. Even you can add condition of marks(not less than 1 or greater than 100) while reading marks.

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26