• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Arraylist/Reading Text Files

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

 
Sheriff
Posts: 9058
667
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)?
 
Marshal
Posts: 81617
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic