Quick question

From main thread

double[] ascore;
int i;
imax=high.highestScore(ascore[],i);

From method highestScore

public class highestScore
{
    // This method checks which number is higher.
public static double highestScore(double[] score,int arraySize)
 {
         int index;
        double maxScore=0;

         for (index=0; index<arraySize; index++)
             {
             if (maxScore < score [index])
              maxScore= score[index];
             }
         return maxScore;
 }
}

I'm trying to pass the array and index number for array length into the method
highestScore to find the highest score and return the value. It's not working for me and I'm not sure the format I should use to pass the values through the method. Can someone show me that?

Recommended Answers

All 5 Replies

If you pass the array as a parameter, you can check its length in the method. score.length will give you what you need. Note that this is a field, not a method.

Out of curiousity, do you ever give "i" a value in your actual code?

Yes I do. in my code

int i=0;
		double[] ascore = new double[8];
		double score, sum=0, imax, imin;
		boolean number;
		String str;
		String output;

			do
			{
			str = JOptionPane.showInputDialog("Enter score " + (i+1));
			number = isNum.isNumeric(str);
			}
			while(number != true);


		do
		{
			ascore[i] = Double.parseDouble(str);
			i++;

			do
			{
			str = JOptionPane.showInputDialog("Enter score " + (i+1) + " (Enter '-9' to end entry):");
			number = isNum.isNumeric(str);
			}
			while(number != true);

		}
		while(!str.equals("-9"));

So then I can write

imax=high.highestScore(ascore[])

and just grab the length inside the highestScore code?

I am getting

C:\Users\Roy\Documents\CISS 110 301\diving2.java:71: '.class' expected
imax=high.highestScore(ascore[]);
                               ^
C:\Users\Roy\Documents\CISS 110 301\diving2.java:72: '.class' expected
imin=low.lowestScore(ascore[]);
                             ^
2 errors

Process completed with exit code 1

When I try to compile it setup like that.

Remove the brackets when you're making the method call.

imin= low.highestScore(ascore);

ascore is the name of the variable, that's how you refer to the array. ascore[n] is a reference to a member of ascore, ascore[] is syntactic sugar for "hey compiler, this is an array!"

Ahhh. Got it. Thank you Jon. I'm marking as solved. My program compiles and executes perfectly. Thanks!

Be a part of the DaniWeb community

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