Spaces:
Running
Running
String row = reader.nextLine(); String[] chunks = row.split(","); Integer.valueOf(chunk);
Browse files
Week 3: Objects, files and exceptions/17a Handling file contents
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
nextLine() - returns String
|
| 2 |
+
valueOf() - Integer OR Double
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
===================================================================
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import java.util.Arrays;
|
| 10 |
+
|
| 11 |
+
public class Example {
|
| 12 |
+
public static void main(String[] args){
|
| 13 |
+
String str = "first,second,third";
|
| 14 |
+
String[] chunks = str.split(",");
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
//print array - [first, second, third]
|
| 18 |
+
System.out.println(Arrays.toString(chunks));
|
| 19 |
+
//print string elements
|
| 20 |
+
for (String chunk : chunks) {
|
| 21 |
+
System.out.println(chunk);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
String sentence ="This is a sentence with many words";
|
| 26 |
+
String[] words = sentence.split(" ");
|
| 27 |
+
System.out.println(Arrays.toString(words));
|
| 28 |
+
for (String word : words) {
|
| 29 |
+
System.out.println(word);
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
Program outputs:
|
| 35 |
+
[first, second, third]
|
| 36 |
+
first
|
| 37 |
+
second
|
| 38 |
+
third
|
| 39 |
+
[This, is, a, sentence, with, many, words]
|
| 40 |
+
This
|
| 41 |
+
is
|
| 42 |
+
a
|
| 43 |
+
sentence
|
| 44 |
+
with
|
| 45 |
+
many
|
| 46 |
+
words
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
===================================================================
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
eg
|
| 58 |
+
luvut/numbers.csv
|
| 59 |
+
1,4,3,5,2,4,5,2
|
| 60 |
+
4,5,11,2,4,2,5,4,3
|
| 61 |
+
6,7,5,2,3,3,2,4,6
|
| 62 |
+
8,7,4,5,4,3,3,2,1
|
| 63 |
+
0,0,2,4,2,0,2,5,3
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
reads the file
|
| 71 |
+
decomposes each line into chunks,
|
| 72 |
+
converts the chunks to integers and
|
| 73 |
+
finally calculates the sum of the numbers:
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
import java.io.FileNotFoundException;
|
| 77 |
+
import java.io.File;
|
| 78 |
+
import java.util.Scanner;
|
| 79 |
+
|
| 80 |
+
public class Example {
|
| 81 |
+
public static void main(String[] args){
|
| 82 |
+
File file = new File("luvut.csv");
|
| 83 |
+
|
| 84 |
+
// read the file
|
| 85 |
+
try(Scanner reader = new Scanner(file)) {
|
| 86 |
+
int sum = 0;
|
| 87 |
+
while (reader.hasNextLine()) {
|
| 88 |
+
|
| 89 |
+
// process line by line
|
| 90 |
+
String row = reader.nextLine();
|
| 91 |
+
// split into an array of number strings
|
| 92 |
+
String[] chunks = row.split(",");
|
| 93 |
+
|
| 94 |
+
// parse each number string as Integer
|
| 95 |
+
for (String chunk : chunks) {
|
| 96 |
+
int num = Integer.valueOf(chunk);
|
| 97 |
+
sum += num;
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
System.out.println("Sum of the numbers: " + sum);
|
| 102 |
+
}
|
| 103 |
+
catch (FileNotFoundException e) {
|
| 104 |
+
System.out.println("Error: " + e.getMessage());
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
Program outputs:
|
| 111 |
+
Sum of the numbers: 159
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|