0

I'm getting a different output.The output I want is- Quizzes: 66% Labs: 88% Lab attendance: 81% Midterms: 91% Final: Not applicable Overall Average: 85%. But I'm getting

output:

[I@176c74b
[I@116471f
[I@1975b59
[I@lee3914

My method normalize is suppose to get the percentage then goes to the average method. I call this method for quizArray,labArray,attendance, midterms to try to get the grades for each of them.

import java.io.*;
import java.util.*;

public class FindGrade {
    public static final int NUM_SCORE_TYPES = 5;

    public static void main(String[] args) {
        Scanner scan = null;
        int[] quizArray = null;
        int[] labArray = null;
        int[] attendance = null;
        int[] midterms = null;
        int quizgrade = 0;
        int labgrade = 0;
        int attendance_1 = 0;
        int midterms_1 = 0;
        String name;


        try {
            scan = new Scanner(new File("input.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }

        // each iteration is for single exam type (ie: Quizzes is the 1st one)
        for (int i = 0; i < NUM_SCORE_TYPES; i++) {

            name = scan.next();
            int numScores = scan.nextInt();
            int maxGrade = scan.nextInt();

            if (name.equals("Quizzes")) {
                quizArray = new int[numScores];
                readScores(quizArray, numScores, scan);


            } else if (name.equals("Labs")) {
                labArray = new int[numScores];
                readScores(labArray, numScores, scan);

            } else if (name.equals("Lab_attendance")) {
                attendance = new int[numScores];
                readScores(attendance, numScores, scan);

            } else if (name.equals("Midterms")) {
                midterms = new int[numScores];
                readScores(midterms, numScores, scan);

            }

        }

    }


    public static void readScores(int[] scoreArray, int numScores, Scanner scan) {
        for (int i = 0; i < numScores; i++) {
            scoreArray[i] = scan.nextInt();
        }
        average(scoreArray, numScores);
    }

    public static int normalize(int[] scoreArray, int maxGrade) {
        int total = 0;
        for (int i = 0; i < scoreArray.length; i++) {
            total += scoreArray[i];
        }
        int percent = Math.round(total * 100 / maxGrade);
        return percent;
    }

    public static double average(double[] scoreArray, int numScores) {
        double sum = 0;
        for (int i = 0; i < scoreArray.length; i++) {
            sum += scoreArray[i];
        }
        double average = sum / numScores;

        return average;


    }

input file:

Quizzes 8 10
5 8 9 10 4 0 10 7
Labs 6  100
95  90  100  87  63  92
Lab_attendance  16  1
1  1  1  0  1  1  1  1  0  1  1  1  1  0  1  1
Midterms  2  100
87  94
Final  0  100
4
  • You are missing one } at end in source code. Commented Feb 9, 2014 at 15:53
  • where is you output? And where do you call the normalize and average method? Commented Feb 9, 2014 at 16:32
  • My output is at the top. Commented Feb 9, 2014 at 16:34
  • I call the average method in readScores but I'm not sure where to call the normalize method Commented Feb 9, 2014 at 17:04

1 Answer 1

1

Each object has toString() method, its default will display the class name representation, then adding @ and then the hashcode. I assume that you're printing System.out.println(someArray).

You should use Arrays#toString() (Below is the implementation of it so you can better understand it):

3860     public static String toString(int[] a) { {
3861        if (a == null)
3862            return "null";
3863        int iMax = a.length - 1;
3864        if (iMax == -1)
3865            return "[]";
3866
3867        StringBuilder b = new StringBuilder();
3868        b.append('[');
3869        for (int i = 0; ; i++) {
3870            b.append(a[i]);
3871            if (i == iMax)
3872                return b.append(']').toString();
3873            b.append(", ");
3874        }
3875    }
Sign up to request clarification or add additional context in comments.

1 Comment

This gives me the entire array. I'm trying to get the percentage: 66%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.