# Swimming competition score program

christopher goodwin
Greenhorn
Posts: 10
Below is my current code for the program.

The assignment is to: Write a program that allows the user to enter the eight judges' scores and
then outputs the points received. The lowest and highest values are not computed.
Everything works entirely except theres one missing section that I can not figure out how to
properly... "say it" you know, proper syntax. I need to limit the values the use can input to a
minimum of 0 and a maximum of 10. I can't figure out which logical statements I can use
with the current code I have, and that may be the problem. If anyone can help me out it would be
GREATLY appreciated.

christopher goodwin
Greenhorn
Posts: 10
by the way, the MAX_VALUE and MIN_VALUE are backwards, just realized that myself

Jonathan Campbell
Greenhorn
Posts: 3
If I understand correctly, you want the judges score (inputted in the for loop) to be between 1 and 10. If you abstract from the actual code, you can realize this is a condition. So what you can do is implement this condition in the relevant part of code. In pseudo-code:

start a loop of 8 cycles
-is input ok?
-if it is ok: do your thing

you can also take another approach:

start a loop of 8 cycles
-while the input is wrong

Hope it helps!

Campbell Ritchie
Sheriff
Posts: 50168
79
Welcome to the Ranch

You need to move the programming out of the main method; in my opinion an ideal main method contains a single statement. Also, try and remove the word static from your vocabulary. If you call something static just so it will compile, you have gotstatic wrong.
To get the input, you create a separate method which runs a loop while the number input is outwith the range 0...10 or whatever. When you get input in the correct range, you can terminate the loop and return the result. You can use another method to validate that input from a Scanner is in the correct format; you will find a rather similar example here.

And after all I have said, those two methods probably ought to be static!

christopher goodwin
Greenhorn
Posts: 10
Thanks for the welcome

I appreciate the help a lot. That is where my issue is..
This is my first program where I have actually had to attempt arrays

I know the program is a little messy, I just wanted to get it working in Main before i tidied it up
and made it look all pretty to hand in xD

I know the pseudo code and the way the logic should be formed, I'm just having the issue of not
being able to figure out the proper Java syntax to add in the condition statement. Let's see if this
idea is on the right track...

Put the for loop in its own method, call the method from the main. Use a while loop(perhaps?) to
check the input going to the for loop, then execute the program, or do else..

I tried this yesterday, but the problem i ran into was that the array values are built into the for loop,
and when i create the while loop to check the input of the array values, the value is unknown, because
it had yet to been defined in the program. Any tips on the best way to handle this? It's most likely
the simplest thing ever but I've just been staring at the code for too long that I can't see the common
sense mistake hahaha. Thanks again for the help!!!

Campbell Ritchie
Sheriff
Posts: 50168
79
• 1
You have still got it the wrong way round. It is a not a case of the array matching the loop. It is a case of making the loop match the array.
You are also not thinking object-oriented. You need a Swimmer class, with the usual fields, including an array for the marks. [That is a gross over-simplification suggesting a person only swims one event, but you can get away with that at this stage.] Now give it methods to fill in the array and to get the averages back. Remember there are several ways to pass an array, including as an array and as varargs. Neither has any requirements about size of the array, so you can have different Swimmer objects with different numbers of marks because there were different sized panels at different events.
You should not think of getting anything working in the main method and translating it. That is a bit like researching how to get to work by walking there . . . and later taking the bus.

christopher goodwin
Greenhorn
Posts: 10
Keep this in mind that I am still pretty new to Java, and again this
is my first time actually working with arrays, this is actually next weeks
assignment I just want to see if I can work it out in my own time instead
of dealing with school's nonsense. xD

My main problem with Java so far is exactly what you said, not thinking object
orientated.. When i originally started programming years ago I used C and C++
so... you get the picture haha. The transition is more difficult then I thought it would
be.

Ok so let's see if this makes more sense...

Create swimmer class to create new swimmer

WHILE input is correct, the input values are attributed to the scores[i] array,
which is attributed to the swimmer class, because it is the new swimmer.

the forLoop method checks the amount of input values and then does it's
calculation

the checkMinMax method removes the highest and lowest numbers.

Main ----
Call the swimmer class to run the methods.
Output the total and the lowest and highest scores.

Please don't crucify me if I still seem to be going backwards.. I'm just REALLY
trying to understand Java as best as i possibly can.

Campbell Ritchie
Sheriff
Posts: 50168
79
chris goooods wrote: . . . years ago I used C and C++
so... you get the picture haha. The transition is more difficult then I thought it would
be.
Lots of people find that transition very difficult. A bit like driving for years and years on four wheels and then trying to ride a motorbike
. . .
Create swimmer class to create new swimmer

ask for input . . .
No. More like this
• Create Swimmer class
• Learn how to get an array into the Swimmer class. Via the constructor, or a setMarks(double[]) method. Or both.
• Test Swimmer class so as to see what happens.
• Create input class. Search this forum for “utility class” and, if you are lucky, you might find examples with input. Find the links I gave you earlier.
• You can find out about arrays in the Java tutorials, and there is an example of using varags to create an array here.
You need to go step by step; get a little bit working and only them move on to the next stage.

christopher goodwin
Greenhorn
Posts: 10
Thanks a lot!!! Im gonna rummage through all of this information and see if i can figure it out

i found this in i think the first link you posted earlier and it's just... incredibly helpful haha

Just made while loops make SO MUCH MORE SENSE. I understood that they do their check first, but i didn't understand
that i should be checking for a false instead of checking if these conditions be true. Now that whole concept just became so
much simpler haha. Thanks again for the links I'm gonna do some work and see if i can get this thing to work out!

Campbell Ritchie
Sheriff
Posts: 50168
79
christopher goodwin wrote:Thanks a lot!!! . . . incredibly helpful haha . . .

Campbell Ritchie
Sheriff
Posts: 50168
79
christopher goodwin wrote: . . .
. . .
That sort of thing would go well inside a method in a utility class. Search this forum for utility class and you find they have one private constructor only (so you can’t create an instance) and all their members are marked static. Examples: 1 2 3 Note those examples probably have different methods from what you want.

christopher goodwin
Greenhorn
Posts: 10
I'm still having a problem with the logic statements in this program.. The input value
has to be between 1-10, and obviously a positive integer value. I have been attempting
to define both of these conditions but the statements are just not working properly. I'm
sure it is one simplest mistakes in just moving a couple lines around, or possibly not, but
I have run out of ideas at this point... Any extra help would be appreciated.
Code below, both classes.

Victor M. Pereira
Ranch Hand
Posts: 50
Why don't you extract methods from the getScores()?

some isValid() method and some getValueUntilCorrect() method?

The isValid method could have different methods that check for different things.

And after you check it correct you could extract class Validator. That will have all validation related methods.

Campbell Ritchie
Sheriff
Posts: 50168
79
That’s a big improvement on what you had before. But there are still problems.

Get rid of the empty constructor. You don’t want it, otherwise you can have a Swimmer with an array looking like this {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}. Similarly get rid of the initialisation in line 8; you only need the declaration.
Add a documentation comment like this to your constructorYou can of course permit an array with other than 8 elements if you think you are allowed, say, 6 scores. You end up writing if (scores.length != 8) {throw new IllegalArgumentException("Eight scores required");} and similarly you can run through a loop for the validation of each individual element.
You have some errors in the getScores method.
• Incorrect naming convention. If it is called getScores it ought simply to return the scores array. You might do well to return scores.clone() to prevent any code outside your class altering those values. Call it enter scores from keyboard or similar.
• It says Integer on line 60 (that should be integer with a small i). So why are you messing about with double arithmetic? You may need doubles for the averaging, but if you declare double sum = 0d;, that is sufficient.
• Your input from the keyboard is probably best done from a static method in a utility class, which will also have a static Scanner field. You can have a method called nextIntInDefinedRangeFromKeyboard(int min, int max) or similar. Then you can see how much simpler your method will become.
• Using myScanner.next() after you have validated (or not validated) the value of an input is incorrect. You have no need to get rid of the next token.
•
christopher goodwin
Greenhorn
Posts: 10
I understand what you're telling me I should do, I've checked out all the links and examples and they're very helpful but I just can not seem to implement
correctly. Any further suggestions or helping me work out the code? The problem is that this is my first semester learning Java... and I'm taking an online
class... Horrible idea.. haha. The only place I'm able to consult any ideas or actually learn anything is online forums or in my the couple books I got.. Just
any more help really working out the code so I can understand this set of statements better and can use them in the future. I know I should make a utility class
but I just need some guidance on the whole way it should be done.. Thanks in advance

Campbell Ritchie
Sheriff
Posts: 50168
79
You have already got 90% of a method to read an int from the keyboard with a Scanner. You can consider enhancing it to return only an int within a certain range, but it is best to use a second method which calls the getIntFromKeyboard method. All those methods should be inside your utility class. Remember, a utility class usually has a private constructor (explanation in the Java Language Specification) and all static members. Example here, but the method is rather different. I can hardly drop any more hints without violating this. You will obviously need different methods. It will look something like thisThat will allow you to see how the methods work, and you can change them as required.

christopher goodwin
Greenhorn
Posts: 10
So after that point, would i make the scores[] array equal to that method, so that they have the same value and run through unhinged?

Campbell Ritchie
Sheriff
Posts: 50168
79
If your scores[] array has 8 elements, you would call the intFromKeyboard method 8 times. You have already got something like that in earlier postings.

christopher goodwin
Greenhorn
Posts: 10
Also, would you suggest rather using exception handling with try and catch blocks? or if else statements..
If statements to me are simple, I've never attempted exception handling yet. Thanks

And just as well, if the utility class is set to private, how do I go about calling the methods to put them into
action??

christopher goodwin
Greenhorn
Posts: 10
Ok, i made the utility class and have it all worked out.. But now I'm getting a NullPointerException..
I've looked as much up as I can find and still cannot figure out the problem that is happening here..
I'll post the code of the utility class method that I'm trying to use and the section of the code where I
am receiving the error....

The if statement there was just something i was trying... Haha.

Campbell Ritchie
Sheriff
Posts: 50168
79
If you can handle things without exceptions, do so.

But . . .
Not happy about those input methods; you had the works of an inputInt method from an earlier link. Why didn’t you use that? Your getIntFromKeyboard method never “exits”. It keeps going until it gets an int to return. It has no sentinel value for exiting. You never get a NumberFormatException from a Scanner. If you go back to that old post, you find it is specifically designed to avoid any Exceptions, so no need for a try-catch. You will also see how much simpler it is than what you have got.
Capital letter to start the names of all classes, please. If you have to hand that in for marking, give the class a more “formal” name than doStuffClass. Put it in a different package from Swimmer. You can then use it again. Your class should not extend anything. (Well, java.lang.Object). Certainly not an exception.