Re: How do i use an ArrayList to store scores and add up scores
Patricia Shanahan wrote:
judith wrote:
hi, I'm needing help with a program that i don't understand how to
store 7 ArrayList scores and sum them up. the program instructions are
as follows.
This program calculates the scores for a contest in diving. The highest
and lowest scores are throwm out. The remaining are added together and
the sum is multiplyed by the degree of difficulty and 0.6. This program
uses and ArrayList to hold the seven scores and then scans the array
for the position of the largest and smallest scores.These positons are
then ignored in computing the sum of the scores.
here is the program and the compile errors that i'm getting. When i use
scores = keyboardnextDouble(); to enter the scores it won't work and
i'm wondering how to write the program so i can enter the scores and
store them. I would appreciate any help that i could get. thank Judith
...
Do you understand the meaning of the error? You are trying to assign the
result of keyboard.nextDouble() to an variable of type ArrayList.
First thing, read the ArrayList documentation. To follow the
instructions literally you will probably need add and get.
Split up the task. You need to get data into an ArrayList, so practice
putting scores you make up in one. When you can do that, try using
scores you get from the console.
Patricia
Patricia or anyone else that can help i'm stuck
I added some things on the program and i'm still getting compile errors
and i don't understand how to fix them any ideas Judith here is the
new program and the errors
//Author: Judith Spurlock
import java.util.ArrayList;
import java.util.Scanner;
//fill in code
public class program5JS
{
public static void main(String[]args)
{
ArrayList<Double> scores = new ArrayList<Double>();
int posMinScore, posMaxScore;
double sum = 0;
double difficulty;
double finalScore;
int i;
Scanner keyboard = new Scanner(System.in);
//Input data values
System.out.println("Enter the degree of difficulty for the dive
(1.2-3.8).");
difficulty = keyboard.nextDouble();
//Input judges scores
double next = 0;
for (i = 1; i <= 7; i++)
{
System.out.println("Enter score for judge " + i + " (0-10).");
next = keyboard.nextLineDouble();//added this and it doesn't work
scores.add(next);
}
//Find position of min score
posMinScore = 0;
for(i = 1; i < scores.size(); i++)
{
if(scores.get(i) < scores.get(posMinScore))
posMinScore = i;
}
scores.remove(posMinScore);
//Find position of max score
posMaxScore = 0;
for(i = 1; i > scores.size(); i++)
{
if(scores.get(i) > scores.get(posMaxScore))
posMaxScore = i;
}
scores.remove(posMaxScore);
//sum scores
for(i = 1; i < scores.size(); i++)
{
scores.add(i);//added this and it doesn't work
sum = sum + i;
}
//Calculate total score
finalScore = difficulty * sum * 0.6;
System.out.println("The diver's final score is " + finalScore);
C:\>javac program5JS.java
program5JS.java:35: cannot find symbol
symbol : method nextLineDouble()
location: class java.util.Scanner
next = keyboard.nextLineDouble();//should be an array of scores
^
program5JS.java:62: cannot find symbol
symbol : method add(int)
location: class java.util.ArrayList<java.lang.Double>
scores.add(i);
^
2 errors
C:\>