• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compile problem at mean array method  RSS feed

 
Aggelos Sfakianos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import acm.program.*;

public class GymnasticsJudge2 extends ConsoleProgram {

public void run() {
int nJudges = readInt ("Enter number of judges: ");
double[] array = new double [nJudges];
for (int i = 0 ; i <nJudges ; i++ ) {
array[i] =readDouble("score for judge " + i + ":") ;
}
}
double mean = (double) meanArray(array);

(29) private static meanArray(double[] array) {
for (int i = 0 ; i <array.length ; i++ ) {
int total= 0;
total += array[i] ;
}
double averagescore = total/ array.length ;
return averagescore;
}
}


THE COMPILER GIVES ME ERROR: LINE29: invalid method declaration; return type required
private static meanArray(double[] array) {
^



HOW CAN I FIX THIS PLEASE???
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggelos Sfakianos wrote:
THE COMPILER GIVES ME ERROR: LINE29: invalid method declaration; return type required


HOW CAN I FIX THIS PLEASE???



The error message is quite clear. Java requires that method declares what it will be returning -- and if it is to return nothing, then the return type is of void. Basically, the return type is required in the method declaration -- and in looking at your code, it looks to be of type double.

Henry
 
Aggelos Sfakianos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed it to double and now it tells me that it cannot find symbol at line 24 :
double mean =meanArray(array);


and also it cannot find symbol at line 35 :

double averagescore = total/ array.length ;

PLEASE HELP

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggelos Sfakianos wrote:I changed it to double and now it tells me that it cannot find symbol at line 24 :
double mean =meanArray(array);


and also it cannot find symbol at line 35 :

double averagescore = total/ array.length ;


No idea what you are talking about. You need to show us the code.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, welcome to the ranch.

Next, there are couple of things you can do to help you have a better experience here. I would STRONGLY suggest you read our HowToAskQuestionsOnJavaRanch FAQ - specifically, EaseUp, TellTheDetails, and UseCodeTags.
 
Aggelos Sfakianos
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the code


http://imageshack.com/a/img11/6321/dv41.png


and here is the error of the compiler

http://imageshack.com/a/img853/6090/ehnr.png
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggelos Sfakianos wrote:here is the code

http://imageshack.com/a/img11/6321/dv41.png


and here is the error of the compiler

http://imageshack.com/a/img853/6090/ehnr.png



What's wrong with posting the code (with code tags) here? There isn't much code to post, for it to be a concern. Anyway, to answer your questions...


Aggelos Sfakianos wrote:I changed it to double and now it tells me that it cannot find symbol at line 24 :
double mean =meanArray(array);


Basically, the compiler is saying that it can't find the "array" variable (that is in scope at the point that you are using it). There is an "array" variable in the run() method, but that variable is a local variable to that method, and can't be accessed outside of that method.


Aggelos Sfakianos wrote:
and also it cannot find symbol at line 35 :

double averagescore = total/ array.length ;


Same reason. There is no such a thing as a "total" variable (that is in scope at the point that you are using it). There is a "total" variable in the same method, but that variable is local to the for-loop, and can't be accessed outside of the for-loop-block.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggelos Sfakianos wrote:here is the code


http://imageshack.com/a/img11/6321/dv41.png


and here is the error of the compiler

http://imageshack.com/a/img853/6090/ehnr.png

PostTextNotScreenshots.

tl;dr version - I can't cut'n'paste your image into my IDE to compile it myself. I can increase the font if you have your actual code posted to let me read it. Your .png file is mostly a blurry haze to my old eyes.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags, as Fred suggested earlier, and removed the worst of the excess blank space and the nasty red text; many people find coloured text difficult to read.

Don't mix types around +=. If you have int on the left and double on the right, you can get all sorts of subtle errors which the compiler will not warn you about.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!