user2961364
user2961364

Reputation: 27

I cannot return a value from my array

This is assessed work so please don't give a straight answer.

My program is supposed to calculate the users grade: "pass" , "fail" or "pass with compensation". However, it doesn't return the answer. I'm unable to figure out why - can anyone help?

public class MarkCalculator { static int[] marks = new int[12];

//public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int weighting;
    int coursework;
    int exammark;

    System.out.println("Please enter course work weighting");
    marks[0]= kb.nextInt();

    System.out.println("Please enter course work mark");
    marks[1]= kb.nextInt();

    System.out.println("Please enter exam mark");
    marks[2]= kb.nextInt();


    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

}




public String[] computeMarks(int[] marks) {

    final int[] WEIGHTING = {55,66,55,44,33,44};
    String[] results = new String[WEIGHTING.length];
    for (int i = 0; i < marks.length / 2; i++) {

        int exam = marks[i];
        int cw = marks[i];
        int weight = WEIGHTING[i];
        int formula = ((cw + weight) + (exam * (100 - weight)) / 100);

        if (formula >= 40){
            results[i]="PASS";
        } else if ((formula < 39) && (formula > 35)){
            results[i]="COMPENSATION PASS";
        }else{
            results[i]="FAIL";

        }

    }

    return results;
}

}

Upvotes: 1

Views: 88

Answers (3)

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

The problem is WEIGHTING is empty

final int[] WEIGHTING = {}; // empty
String[] results = new String[WEIGHTING.length];
for (int i = 0; i < marks.length / 2; i++) {

    int exam = marks[i];
    int cw = marks[i];
    int weight = WEIGHTING[i]; // You cant access elements from an empty array

Also

    MarkCalculator mc = new MarkCalculator();
    mc.computeMarks(marks);

here you are passing marks which is empty.

EDIT

Reason why your program wasnt working is because you are not catching the result from computeMarks. You should store it in an array inside main like

String[] result = mc.computeMarks(marks);

for(int k=0;k<result.length;k++)
{
    System.out.println(result[k]);
}

Upvotes: 1

wxyz
wxyz

Reputation: 707

Also: you don't initialize marks with anything. Try this:

System.out.println("Please enter course work weighting");
        marks[0]= kb.nextInt();

        System.out.println("Please enter course work mark");
        marks[1]= kb.nextInt();

        System.out.println("Please enter exam mark");
        marks[2]= kb.nextInt();

        MarkCalculator mc = new MarkCalculator();
        mc.computeMarks(marks);

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 208984

final int[] WEIGHTING = {};
String[] results = new String[WEIGHTING.length];

Here's a problem. WEIGHTING has no initial size.

Upvotes: 3

Related Questions