Question:
java question that i need help on?
2012-12-21 12:55:05 UTC
ok . i'm trying to create a program that gives out

Contestant 1
8.4 9.1 8.5 8.4 9.1 8.7 8.8 9.1 =8.77

Contestant 2
7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 =7.00

Contestant 3
8.0 7.9 8.0 8.0 8.0 8.0 8.0 8.1 =8.00

where it adds all the double numbers while taking away the high est and lowest of the scores , and averaging the total. this is what i have for the contestant 1 . can you tell me what i did wrong and what i need to fix for this one and how to tackle the next two contestants. ?

public static void main(String [] args)
{
Scanner inFile=null;
try
{
inFile=new Scanner(new File("prog215h.dat"));
}
catch(FileNotFoundException e)
{
System.out.println("File not found! ");
System.exit(0);
}
while (inFile.hasNext())
{
double file=inFile.nextDouble();
}
constent1(file);
}

public static void contestent1(double file)
{
DecimalFormat f=new DecimalFormat("#.00");
while (inFile.hasNext())
{
double max=0,small=0,big=0;
for (int count=0;count<=7;count++)
{
System.out.print(file+" ");
max+=file;
if (count==0)
{
small=file;
big=file;
}
if (count>0)
{
if (small>file)
{small=file;}
if (big {big=file;}
}
}
double answer=max-(small+big);
System.out.print(" = "+f.format(answer/6));
}
}

I need some help on the method connecting . for some reason the methods are not connecting with each other.
Three answers:
Kaydell
2012-12-22 11:30:45 UTC
Hi,



I can see that you've put in the effort to write some code, so I'll write some code too for you.



Here, this is closer to what you want. In my version of the code, I read from the text file in a loop, reading one line at-a-time, and then pass each line on to the average method which will read one double at a time, and average the scores throwing out the highest and lowest.



Take a look at my code and read the comments that I've written in it:

http://ideone.com/ucj7El
?
2012-12-21 21:17:01 UTC
This code is not going to work. It is badly planned. I would suggest heading back to the drawing board. First big error is then nam,e of the method you are calling in main and the name of the method you are trying to call are not the same...you have a spelling error.



You are also only going to call your contestant method once and from the looks of it you mean to call it over and over as long as there is data to be read. That isn't going to happen since your method call is not in the loop. Even if it did happen, you pass in one number at a time, your contestant method would run and return to main and all of the variables in that method would go away once the contestant method ends. You are not going to be able to accumulate your values like you want.



Not knowing exactly how your data file is setup, I would suggest reading in all of the contestant's numbers into something like an array or a list then passing that to the method which strips out the high and low and sums the rest. That method could return the average for the contestant. This way you only need to write one method not matter how many contestants are in your data file.
Oct9gon
2012-12-21 20:55:55 UTC
I think you forgot a brace


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...