简体   繁体   中英

Method cannot be applied to given types in java

I'm writing a program for a class that uses data from a file (students' grades) and then averages and organizes it based on highest grade to lowest. I'm almost done except for an error at line 46, where it won't call the method: findMaxIndex(courseGrade) ;

Any help would be greatly appreciated!

package project3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 *
 * @author badluckbowers
 */
public class Project3 {

    final static int NUM_STUDENTS = 16;
    public static String[] nameArray = new String[NUM_STUDENTS];
    public static double[] labAvg = new double[NUM_STUDENTS];
    public static double[] quizAvg = new double[NUM_STUDENTS];
    public static double[] projectAvg = new double[NUM_STUDENTS];
    public static double[] examAvg = new double[NUM_STUDENTS];
    public static double[] finalExamArray = new double[NUM_STUDENTS];
    public static double[] courseGrade = new double[NUM_STUDENTS];
    public static char[] gradeArray = new char[NUM_STUDENTS];

    /**
     * @param args the command line arguments
     */
    final static int numLabScores = 15;
    final static int pointsLabPossible = 10;
    final static int numQuizScores = 12;
    final static int pointsQuizPossible = 5;
    final static int numProjectScores = 6;
    final static int pointsProjectPossible = 25;
    final static int numExamScores = 2;
    final static int pointsExamPossible = 100;
    final static int numFinalExamScores = 1;
    final static int pointsFinalExamPossible = 100;

    public static void main(String[] args) throws FileNotFoundException {

        readFile("scores.txt", nameArray, labAvg, quizAvg, projectAvg, examAvg, finalExamArray);

        findMaxIndex(courseGrade); 

        printArray();

    }

    public static void readFile(String fileName, String[] nameArray, double[] labAvg, double[] quizAvg, double[] projectAvg, double[] examAvg, double[] finalExamArray) throws FileNotFoundException {
        File input = new File(fileName);
        if (!input.exists()) {
            System.out.println("Error opening scores.txt for input; "
                    + "aborting program run.");
            System.exit(0);
        }
        Scanner inputFile = new Scanner(input);

        for (int i = 0; i < NUM_STUDENTS; i++) {
            nameArray[i] = inputFile.nextLine();
            labAvg[i] = calculatePercent(inputFile, numLabScores, pointsLabPossible); //15-1
            quizAvg[i] = calculatePercent(inputFile, numQuizScores, pointsQuizPossible);  //12-1
            projectAvg[i] = calculatePercent(inputFile, numProjectScores, pointsProjectPossible);  //6-1
            examAvg[i] = calculatePercent(inputFile, numExamScores, pointsExamPossible); //2-1
            finalExamArray[i] = calculatePercent(inputFile, numFinalExamScores, pointsFinalExamPossible);  //1-1
            courseGrade[i] = calculateGrade(labAvg[i], quizAvg[i], projectAvg[i], examAvg[i], finalExamArray[i]);
            gradeArray[i] = calculateLetter(courseGrade[i]);

            inputFile.nextLine();

        }

        inputFile.close();

    }

    public static double calculatePercent(Scanner inFile, int numScores, int pointsPossible) {
        double score;
        double total = 0;

        for (int i = 0; i < numScores; i++) {
            score = inFile.nextDouble();
            total += score;
        }
        return (total / (numScores * pointsPossible)) * 100;
    }

    public static double calculateGrade(double labAvg, double quizAvg, double projectAvg, double examAvg, double finalExamArray) {

        return ((labAvg * .15 + quizAvg * .10 + projectAvg * .25 + examAvg * .30 + finalExamArray * .20));

    }

    public static char calculateLetter(double courseGrade) {
        if (courseGrade < 60.0) {
            return 'F';
        } else if (courseGrade >= 60.0 && courseGrade < 70.0) {
            return 'D';
        } else if (courseGrade >= 70.0 && courseGrade < 80.0) {
            return 'C';
        } else if (courseGrade >= 80.0 && courseGrade < 90) {
            return 'B';
        }
        return 'A';

    }

    //__________________________________________________________________________
    // sort stuff
    /**
     * finds index of smallest element in the part of the array bounded by start
     * and array.length-1
     *
     * @param courseGrade is fully populated
     * @param start is index to begin searching for smallest element
     * @return index of smallest item between start and array.length-1
     * @throws java.io.FileNotFoundException
     */
    public static double findMaxIndex(double[] courseGrade, int start) throws FileNotFoundException {
        int maxIndex;

        maxIndex = start;

        for (int index = start + 1; index < courseGrade.length; index++) {
            if (courseGrade[index] > courseGrade[maxIndex]) {
                maxIndex = index;
            }
        }

        return maxIndex;
    }

    /**
     * the items in the array will be sorted largest to smallest
     *
     * @param courseGrade
     * @throws java.io.FileNotFoundException
     */
    public static void sortArray(double[] courseGrade) throws FileNotFoundException {
        int maxIndex;
        double maxItem;

        for (int i = 0; i > courseGrade.length - 1; i++) {
            maxIndex = (int) findMaxIndex(courseGrade, i);

            //put the smaller item in place of the current item
            maxItem = courseGrade[maxIndex];
            courseGrade[maxIndex] = courseGrade[i];
            courseGrade[i] = maxItem;

            System.out.println("Array after pass " + (i + 1) + " of selection sort:");
            for (int j = 0; j < courseGrade.length; j++) {
                System.out.print(courseGrade[j] + "  ");
            }
            System.out.println("");

        }
    }

    //__________________________________________________________________________
    public static void printArray() {
        System.out.println("The array elements are: ");
        for (int i = 0; i < nameArray.length; i++) {
            System.out.println(nameArray[i] + "  " + labAvg[i] + "  " + quizAvg[i] + "  " + projectAvg[i] + "  " + examAvg[i] + "  " + finalExamArray[i] + "  " + courseGrade[i] + "  " + gradeArray[i]);
        }
        System.out.println();
    }

}

You need to pass second argument when calling the function findMaxIndex . Because your function definition has two parameters.

  findMaxIndex(courseGrade, startIndex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM