Hi everyone, I have a question on java.util.Scanner.
My code is :
When the input is entered in a single line as : 1 2 3 4 5 6 7 8 , it calculates correct. But if there is a new line carrier between lines (enter) the calculation look wrong. So how I can ignore enter's and calculate summary of input regardless they are entered in a single or multiple lines?
Mathew Brown wrote: . . .
When the input is entered in a single line as : 1 2 3 4 5 6 7 8 , it calculates correct. . . .
Not when I tried it, it didn’t
$ java Judging
Enter the 8 judges' scores please(0-10).
7 2 9 10 7 0 7 2
Contestant's final score is:
Contestant's highest score is:
Contestant's lowest score is:
Contestant's total score is:
Surely final score should have been 34? You are not calculating highest and lowest correctly. I tried with the same eight marks each on a line by itself and the output was unchanged. Please explain what you mean by “look wrong”. I do not think the error has anything to do with the Scanner.
Have you really been taught to put so much code in the main method? The main method is there to start your application off, not to do all the work. At least 90% of that code needs to come out of there.
You are not using object‑orientation. Create a Scores class which has the scores array as a field. Give it separate methods which calculate totals, highest, lowest, average, etc. Test all those methods in turn.
Do not write one large method; that is very difficult to test. A method should do one thing and one thing only. Then you can check that it is doing it correctly.
For a slightly more advanced technique, but one which is more reliable, search my posts in this forum and “Java in General” for “Scanner Utility Class”, and you will find (incomplete) utility classes which you can put together, and call KeyboardInputs.getDoubleInspecifiedRange(...); You can then get the Scanner right out of your current class.
Mathew Brown wrote:Hi everyone, I have a question on java.util.Scanner...
Well this isn't going to answer it but, like Campbell, I have a word of advice:
highest=highest<calculateScore[index]?calculateScore[index]:calculateScore; (line 36)
is just horrible to look at; particularly as '<' has more than one use in Java.
I replaced calculateScore respectfully with "highest" and "lowest" also added some null space between the operators in comparison string. Now code looks working perfectly. I will also try to add additional methods to empty out main method as Campbell suggested. Thanks all for your valuable inputs. I am a java newbie and try to get better.