Spaces:
Running
Running
Create 17b. Read numbers from a file
Browse files
Week 3: Objects, files and exceptions/17b. Read numbers from a file
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Numbers are stored in the file numbers.csv.
|
| 2 |
+
One line contains several numbers separated by commas.
|
| 3 |
+
The file could look like this:
|
| 4 |
+
1,2,1,2,3
|
| 5 |
+
4,3,2,3,2,4,2
|
| 6 |
+
1,2,3
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
Write the method
|
| 10 |
+
|
| 11 |
+
ArrayList<Integer> allValues()
|
| 12 |
+
|
| 13 |
+
which reads the numbers from the file, and stores them in a list.
|
| 14 |
+
Finally, the list is returned.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
Example method call:
|
| 19 |
+
public static void main(String[] args){
|
| 20 |
+
ArrayList<Integer> list = allValues();
|
| 21 |
+
System.out.println(list);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
Example output:
|
| 25 |
+
[1, 2, 1, 2, 3, 4, 3, 2, 3, 2, 4, 2, 1, 2, 3]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
==================================
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
import java.util.Random;
|
| 33 |
+
import java.util.ArrayList;
|
| 34 |
+
import java.io.FileNotFoundException;
|
| 35 |
+
import java.util.Scanner;
|
| 36 |
+
import java.io.File;*/
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
public class Test {
|
| 41 |
+
public static void main(String[] args){
|
| 42 |
+
final Random random = new Random();
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
System.out.println("File:");
|
| 46 |
+
for (String s : input) {
|
| 47 |
+
System.out.println("" + s);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
ArrayList<String> list = new ArrayList<>();
|
| 52 |
+
Scanner scanner = new Scanner(System.in);
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
System.out.println("Testing the file's numbers...");
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
ArrayList<Integer> numbers = allValues();
|
| 59 |
+
System.out.println("All values:");
|
| 60 |
+
for (int value : numbers) {
|
| 61 |
+
System.out.println(value);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
//ADD
|
| 67 |
+
public static ArrayList<Integer> allValues() {
|
| 68 |
+
ArrayList<Integer> list = new ArrayList<>();
|
| 69 |
+
|
| 70 |
+
try {
|
| 71 |
+
Scanner reader = new Scanner(new File("numbers.csv"));
|
| 72 |
+
// read each line
|
| 73 |
+
while (reader.hasNextLine()) {
|
| 74 |
+
|
| 75 |
+
String line = reader.nextLine();
|
| 76 |
+
// split line into array of Integer strings
|
| 77 |
+
String[] values = line.split(",");
|
| 78 |
+
for (String value : values) {
|
| 79 |
+
list.add(Integer.valueOf(value));
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
catch (FileNotFoundException e) {
|
| 84 |
+
System.out.println("Error: numbers.csv file not found");
|
| 85 |
+
}
|
| 86 |
+
return list;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
File:
|
| 100 |
+
431,668,669,425,27,491,241
|
| 101 |
+
751,718,167,454,665,251,958
|
| 102 |
+
218,887,235,954,948,947,763
|
| 103 |
+
122,306,137,719,905,921,349
|
| 104 |
+
Testing the file's numbers...
|
| 105 |
+
All values:
|
| 106 |
+
431
|
| 107 |
+
668
|
| 108 |
+
669
|
| 109 |
+
425
|
| 110 |
+
27
|
| 111 |
+
491
|
| 112 |
+
241
|
| 113 |
+
751
|
| 114 |
+
718
|
| 115 |
+
167
|
| 116 |
+
454
|
| 117 |
+
665
|
| 118 |
+
251
|
| 119 |
+
958
|
| 120 |
+
218
|
| 121 |
+
887
|
| 122 |
+
235
|
| 123 |
+
954
|
| 124 |
+
948
|
| 125 |
+
947
|
| 126 |
+
763
|
| 127 |
+
122
|
| 128 |
+
306
|
| 129 |
+
137
|
| 130 |
+
719
|
| 131 |
+
905
|
| 132 |
+
921
|
| 133 |
+
349
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|