How do i use an ArrayList to store scores and add up scores

J

judith

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

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

for (i = 1; i <= 7; i++)
{
System.out.println("Enter score for judge " + i + " (0-10).");
scores = keyboard.nextDouble();
}
}

}

//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)//doesn't work how do i use the Array list
to store the scores?

//sum scores
for(i = 1; i < scores.size(); i++)
{
// fill in code
}

//Calculate total score
finalScore = difficulty * sum * 0.6;
System.out.println("The diver's final score is " + finalScore;

This is part of what it should look like but i don't know how to input
the scores in an ArrayList and sum them up

C:\>java program5JS
Enter the degree of difficulty for the dive (1.2-3.8).
1.2
Enter score for judge 1 (0-10).
2
Enter score for judge 2 (0-10).
4
Enter score for judge 3 (0-10).
5
Enter score for judge 4 (0-10).
7
Enter score for judge 5 (0-10).
8
Enter score for judge 6 (0-10).
6
Enter score for judge 7 (0-10).
5

Theese are the compile errors

C:\>javac program5JS.java
program5JS.java:31: incompatible types
found : double
required: java.util.ArrayList<java.lang.Double>
scores = keyboard.nextDouble();
^
1 error

C:\>javac program5JS.java
program5JS.java:38: 'class' or 'interface' expected
posMinScore = 0;
^
program5JS.java:39: 'class' or 'interface' expected
for(i = 1; i < scores.size(); i++)
^
program5JS.java:48: 'class' or 'interface' expected
for(i = 1; i > scores.size(); i++)
^
3 errors
 
Last edited by a moderator:
Y

Ye Dafeng

I think you can use BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in)) to read the input;
then, you can use the insert-sort algorithm to store the input data (I
suggest you use the LinkedList).
 
Last edited by a moderator:
P

Patricia Shanahan

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
 
Last edited by a moderator:
J

judith

I'm not understanding the error messages either can anyone suggest or
help in explaining what class or interface expected means? and i'm
still not understanding how to store the scores in an arrayList when
entering them into the computer or how to add them up

these are the new errors



C:\>javac program5JS.java
program5JS.java:40: 'class' or 'interface' expected
posMinScore = 0;
^
program5JS.java:41: 'class' or 'interface' expected
for(i = 1; i < scores.size(); i++)
^
program5JS.java:50: 'class' or 'interface' expected
for(i = 1; i > scores.size(); i++)
^
program5JS.java:65: 'class' or 'interface' expected
System.out.println("The diver's final score is " + finalScore);
^
4 errors

C:\>
 
Last edited by a moderator:
J

judith

I'm not understanding the error messages either can anyone suggest or
help in explaining what class or interface expected means? and i'm
still not understanding how to store the scores in an arrayList when
entering them into the computer or how to add them up

these are the new errors



C:\>javac program5JS.java
program5JS.java:40: 'class' or 'interface' expected
posMinScore = 0;
^
program5JS.java:41: 'class' or 'interface' expected
for(i = 1; i < scores.size(); i++)
^
program5JS.java:50: 'class' or 'interface' expected
for(i = 1; i > scores.size(); i++)
^
program5JS.java:65: 'class' or 'interface' expected
System.out.println("The diver's final score is " + finalScore);
^
4 errors

C:\>
 
Last edited by a moderator:
P

Patricia Shanahan

judith wrote:
....
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 ....
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

What do you think "cannot find symbol" might mean? What steps have you
taken to investigate this message?

Patricia
 
D

Daniel Pitts

judith said:
hi, I'm needing help with a program that i don't understand how to
From a few of your posts, I can see you need help with Java for a
class. Do you want to know Java, or do you want a good grade from the
class? Either way, I suggest you look into a private tutor, your
questions, while valid, are very basic and would be solvable by reading
a book, attending a letchure, or talking with a tutor.

Please, don't post and repost your code over and over again, it begins
to become repeative repeatedly.

Speed some time and look at what the compiler is telling you. The
error messages tell you quite clearly what it expects, you have to
figure out either why it expects that, (maybe a missed parenthesis,
brace, bracket, or something es), or figure out how to give it what it
wants. Don't just give up because you have an error message.

Best of luck,
Daniel.
 
O

Oliver Wong

judith said:
I'm not understanding the error messages either can anyone suggest or
help in explaining what class or interface expected means?

It means the compiler is expecting the keyword "class" or "interface" at
that point, and was surprised to see something else there. "class" and
"interface" themselves are one of the more essential concepts in Java, so if
you don't know what they refer to, you need to slow down and review past
lessons. If you have a textbook, go back to a point where you felt
comfortable, and start re-reading from there. If you didn't feel comfortable
with any of it, then you'll have to start over (or get a different
textbook).

You may also find this online tutorial helpful:
http://java.sun.com/docs/books/tutorial/ Start with the section entitled
"Trails Covering the Basics" and do the lessons in order. Don't skip ahead
until you've completely understood the previous lesson. If you're stuck on a
lesson, make another post citing which lesson you're currently working on
(provide a link to the webpage), and describing your problem (e.g. is it
that you don't understand what's being said in a particular paragraph? Is it
that you can't solve one of the exercises? Something else?)

- Oliver
 
I

Ian Wilson

judith said:
I'm not understanding the error messages either can anyone suggest or
help in explaining what class or interface expected means? and i'm
still not understanding how to store the scores in an arrayList when
entering them into the computer or how to add them up

these are the new errors



C:\>javac program5JS.java
program5JS.java:40: 'class' or 'interface' expected
posMinScore = 0;
^

Tidy up your indentation and all will become clear. Really!
Every time you have a { increase indentation by 4 spaces.
Every time you have a } decrease indentation by 4 spaces.
When a statement runs over into a new line, indent the continuation
by an extra 8 spaces.

I'm over-simplifying but ...
Remember that statements can only occur inside methods.
Declarations can only occur inside classes.

Indentation is a great help. If you are not using an IDE or editor that
does auto-indentation for you, I suggest you obtain a program that will
re-indent your program for you. I googled for "indent java source code"
and the first result was JIndent (I've not tried it)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,726
Latest member
UlrichGrun

Latest Threads

Top