• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help creating a loop to print out total scores

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the text file?

Jackson Lawton wrote:but don't ask me what I did


What did you do?
 
Joey McGee
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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


Add the following:

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joey McGee
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic