Arraylist/Reading Text Files
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Design and implement a program to process golf scores. The scores of four golfers are stored in a text file. Each line represents one hole, and the file contains 18 lines. Each line contains five values: par for the hole followed by the number of strokes each golfer used on that hole. Store the totals for par and the players in an arraylist. Determine the winner and produce a table showing how well each golfer did (compared to par)
I am very confused on reading a text file and creating an array list. I have created the file and did the file part. Its name is golfscores.txt
This is all I have so far..
I am very confused on reading a text file and creating an array list. I have created the file and did the file part. Its name is golfscores.txt
This is all I have so far..
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to the Ranch.
As you noticed already, this is a complex task which requires to accomplish multiple subtasks.
As many subtasks there going to be, as many methods you'll need to write and probably even more - so you could cope with its complexity by solving one subtask at a time.
Probably you can start writting a method, which reads a file (line by line) and prints that out. You have some code for it already, the problem is that it sits in a main method. Can you move it out to an appropriate method? (with an appropriate name for it).
Why you named your variable 'Url'? What the golfers scores have to do with URL's (i.e.: https://coderanch.com)?
As you noticed already, this is a complex task which requires to accomplish multiple subtasks.
As many subtasks there going to be, as many methods you'll need to write and probably even more - so you could cope with its complexity by solving one subtask at a time.
Probably you can start writting a method, which reads a file (line by line) and prints that out. You have some code for it already, the problem is that it sits in a main method. Can you move it out to an appropriate method? (with an appropriate name for it).
Why you named your variable 'Url'? What the golfers scores have to do with URL's (i.e.: https://coderanch.com)?
posted 9 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome again
I presume <String> list in line 12 is supposed to read List<String> list...
Liutauras is correct: create a method to do the reading. Good start printing the line; that way you can see what you have read. Make sure to close all Scanners except those pointing to System.in, and the best way to do that (Java7+) is with try with resources. Once you know you are reading the lines, consider what to do with them; there are many things you can try, adding to a List being one of them. I would suggest you match the while and the reading: while (myScanner.hasNextLine())... to match nextLine().
I presume <String> list in line 12 is supposed to read List<String> list...
Liutauras is correct: create a method to do the reading. Good start printing the line; that way you can see what you have read. Make sure to close all Scanners except those pointing to System.in, and the best way to do that (Java7+) is with try with resources. Once you know you are reading the lines, consider what to do with them; there are many things you can try, adding to a List being one of them. I would suggest you match the while and the reading: while (myScanner.hasNextLine())... to match nextLine().
| Happily living in the valley of the dried frogs with a few tiny ads. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











