Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.Scanner question  RSS feed

 
Mathew Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • 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.
 
Devarsh Shah
Greenhorn
Posts: 6
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is wrong please check logic, it should be :



 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!