Spaces:
Running
Running
Scanner reader = new Scanner(new File("fileName.txt")); list.add(reader.nextLine());
Browse files
Week 3: Objects, files and exceptions/16b. Players from FILE
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The file players.txt contains the names of the players, one name on each line.
|
| 2 |
+
|
| 3 |
+
Write the method
|
| 4 |
+
|
| 5 |
+
ArrayList<String> readPlayers()
|
| 6 |
+
|
| 7 |
+
which reads the contents of the file and stores it in a list,
|
| 8 |
+
so that each name is one element in the list.
|
| 9 |
+
Finally, the list is returned.
|
| 10 |
+
|
| 11 |
+
Use the Scanner class to read the file.
|
| 12 |
+
You can see a sample tutorial in the code above - 16a.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
An example of calling the method
|
| 17 |
+
(note that the test program will draw a new set of contents for the file at each run - so the output will vary):
|
| 18 |
+
public static void main(String[] args){
|
| 19 |
+
ArrayList<String> list = readPlayers();
|
| 20 |
+
System.out.println(list);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
Example output:
|
| 24 |
+
[Ann, Ann, Adam, Keith, Ann, Jane, Thomas, Jack]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
==============================================================================
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
import java.util.Random;
|
| 37 |
+
import java.util.ArrayList;
|
| 38 |
+
import java.io.FileNotFoundException;
|
| 39 |
+
import java.util.Scanner;
|
| 40 |
+
import java.io.File;*/
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
public class Test {
|
| 45 |
+
public static void main(String[] args){
|
| 46 |
+
final Random r = new Random();
|
| 47 |
+
|
| 48 |
+
ArrayList<String> list = new ArrayList<>();
|
| 49 |
+
Scanner reader = new Scanner(System.in);
|
| 50 |
+
|
| 51 |
+
System.out.println("Testing reading the file...");
|
| 52 |
+
ArrayList<String> players = readPlayers();
|
| 53 |
+
System.out.println("Players in list:");
|
| 54 |
+
for (String player : players) {
|
| 55 |
+
System.out.println(player);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
//add
|
| 62 |
+
public static ArrayList<String> readPlayers() {
|
| 63 |
+
ArrayList<String> list = new ArrayList<>();
|
| 64 |
+
|
| 65 |
+
try {
|
| 66 |
+
// NEED TO DEFINE ANOTHER SCANNER INSIDE TO READ FROM THE FILE!!!
|
| 67 |
+
Scanner reader = new Scanner(new File("players.txt"));
|
| 68 |
+
|
| 69 |
+
while (reader.hasNextLine()) {
|
| 70 |
+
list.add(reader.nextLine());
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
catch (FileNotFoundException e) {
|
| 74 |
+
System.out.println("Error: players.txt file not found");
|
| 75 |
+
}
|
| 76 |
+
return list;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
}
|