Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
judith - 15 Nov 2006 23:13 GMT
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

//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

    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
Ye Dafeng - 16 Nov 2006 01:41 GMT
> 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
[quoted text clipped - 119 lines]
>         ^
> 3 errors

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).
Patricia Shanahan - 16 Nov 2006 01:58 GMT
> 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
[quoted text clipped - 11 lines]
> 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
judith - 16 Nov 2006 03:55 GMT
> > 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
[quoted text clipped - 24 lines]
>
> 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:\>
Patricia Shanahan - 16 Nov 2006 04:26 GMT
...
> 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
judith - 16 Nov 2006 02:32 GMT
> 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
[quoted text clipped - 118 lines]
>         ^
> 3 errors

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:\>
judith - 16 Nov 2006 02:32 GMT
> 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
[quoted text clipped - 118 lines]
>         ^
> 3 errors

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:\>
Oliver Wong - 16 Nov 2006 14:07 GMT
> 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
Ian Wilson - 16 Nov 2006 16:44 GMT
> I'm not understanding the error messages either can anyone suggest or
> help in explaining what class or interface expected means? and i'm
[quoted text clipped - 7 lines]
>         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)
Daniel Pitts - 16 Nov 2006 05:30 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.