• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist/Reading Text Files  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!