*
The moose likes Beginning Java and the fly likes Need help creating a loop to print out total scores Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help creating a loop to print out total scores" Watch "Need help creating a loop to print out total scores" New topic
Author

Need help creating a loop to print out total scores

Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
I'm taking Java in school and I'm still very raw when it comes to this stuff. I have a programming problem for homework that is to design and implement a program to process golf scores. The scores of four golfers are stored in a text file. Each line is represents one hole, and the file contains 18 lines. Each line

Now here is my code so far (which took me several good hours to create), but don't ask me what I did because all I know is I used some sort of arrays and loops as I don't fully understand what I all did:

I believe all I have to do now is create a loop to print out the total scores and then somehow show who the winner is. I'm not sure how to do that or even where to put it in my program, so help with that is greatly appreciated.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

Could you post the text file?
Jackson Lawton wrote:but don't ask me what I did

What did you do?
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Joel Christophel wrote:Could you post the text file?


Here is what I put in the text file, which I used Notepad++ for:

Par Jeff Chuck Jackson Jack
3 1 2 3 4
5 2 3 4 5
4 3 4 5 6
4 2 3 4 5
5 4 4 5 4
4 5 6 6 5
5 5 6 6 6
3 3 4 5 5
2 2 3 4 3
4 5 4 4 5
3 4 3 5 5
4 3 4 6 5
5 5 6 6 6
3 4 4 4 3
3 3 5 4 4
4 4 4 5 4
4 5 6 6 6
5 5 5 6 6

The way it shows up on here isn't the way it shows up in the text file as in the text file in Notepad++ it lines the names up with the scores.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

Change this line:
scoreSheet[player][hole] += Integer.parseInt(scoresForOneHole[player]);

To this:
scoreSheet[player][hole] += Integer.parseInt(scoresForOneHole[player].trim());

There were extra spaces after your last column of scores, throwing a runtime exception when you tried to parse them as ints.

Now, to print out the total score at the bottom of each column, add the following to the end of your main method:
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
Joel Christophel wrote:Change this line:
scoreSheet[player][hole] += Integer.parseInt(scoresForOneHole[player]);

To this:
scoreSheet[player][hole] += Integer.parseInt(scoresForOneHole[player].trim());

There were extra spaces after your last column of scores, throwing a runtime exception when you tried to parse them as ints.

Now, to print out the total score at the bottom of each column, add the following to the end of your main method:


Thanks so much but how do you then get it to show who the winner is???
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

Jackson Lawton wrote:
Thanks so much but how do you then get it to show who the winner is???

Add the following:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Here is my advice.

delete every line of code you have.

what you have done is called 'programming by accident'. You wrote something, saw it didn't work, tried something else, and repeated until it sort of, kind of, did what you want. That is the worst way to code. You will never, ever, be able to write anything remotely complicated that way.

The proper way is to turn off you computer. Start by THINKING about the problem. Think about it in English. Literally, write down what the steps that YOU would do if you had to solve this by hand - no computer, no variables, no 'for-loops' - just paper, pencil, and erasers.

THAT is the correct way to program.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joey McGee
Greenhorn

Joined: Nov 22, 2012
Posts: 27
fred rosenberger wrote:Here is my advice.

delete every line of code you have.

what you have done is called 'programming by accident'. You wrote something, saw it didn't work, tried something else, and repeated until it sort of, kind of, did what you want. That is the worst way to code. You will never, ever, be able to write anything remotely complicated that way.

The proper way is to turn off you computer. Start by THINKING about the problem. Think about it in English. Literally, write down what the steps that YOU would do if you had to solve this by hand - no computer, no variables, no 'for-loops' - just paper, pencil, and erasers.

THAT is the correct way to program.


You're absolutly right, but what if you don't even know where to start and you have a program to create for school??? Sometimes it is better to look through-out your book, try there examples and work them out to fit your situation as that way you're putting to practice what the book is teaching you and it makes your work go much faster, especially when you have 10 programs or more to write. Sitting down and over thinking about the problem that you may not understand would create more time spent on that problem and with working 35 hours a week and 4 nights of school on top of that, it doesn't work out well. Maybe if you are programming as a professional or a hobbiest, or you aren't working and going to school both full-time, that makes total sense. I do agree that's the better way to go but time is so precious to me right now that this is the best way to get my assignments done on time.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Jackson Lawton wrote:You're absolutly right, but what if you don't even know where to start and you have a program to create for school??? Sometimes it is better to look through-out your book, try there examples and work them out to fit your situation as that way you're putting to practice what the book is teaching you and it makes your work go much faster, especially when you have 10 programs or more to write. Sitting down and over thinking about the problem that you may not understand would create more time spent on that problem and with working 35 hours a week and 4 nights of school on top of that, it doesn't work out well. Maybe if you are programming as a professional or a hobbiest, or you aren't working and going to school both full-time, that makes total sense. I do agree that's the better way to go but time is so precious to me right now that this is the best way to get my assignments done on time.

So...your suggestion for someone is school is that they not learn what they are doing? In an English Comp class, should they just put random words on the page, not really understand what they mean or how they fit together? Should they just pick sentences from examples of how to write, and hope that somehow, it makes sense and conveys the right idea?

Why is programming any different? If I were teaching, I would much rather someone turned in an incomplete program and understand what they are doing than turn in a hot mess and literally say "I have no idea what I did or how it works".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Jackson Lawton wrote: . . . time is so precious to me right now that this is the best way to get my assignments done on time.
Fred is absolutely right. Also, your guessing technique will produce messy‑looking code which will gain you few marks.

And the method of working out code on paper first will almost certainly take much less time than repeatedly guessing.
 
wood burning stoves
 
subject: Need help creating a loop to print out total scores
 
Similar Threads
How can I add elements in Hashmap?
Cannot throw exception
write to console after writing to file
Help with toString message
IO / Serialization