• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

java.util.Scanner question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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:
35.0
Contestant's highest score is:
7.0
Contestant's lowest score is:
2.0
Contestant's total score is:
44.0

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.
 
Greenhorn
Posts: 6
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is wrong please check logic, it should be :



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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[0];
(line 36)
is just horrible to look at; particularly as '<' has more than one use in Java.

highest = highest < calculateScore[index] ? calculateScore[index] : calculateScore[0];

or even

highest = highest < calculateScore[index]
   ? calculateScore[index]
   : calculateScore[0];


is an awful lot easier to read; and the first one is also consistent with the spacing in line 35, right above it.

Winston


 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you really setting highest back to the element 0???
 
Mathew Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys;


I replaced calculateScore[0] 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.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic