File size: 2,267 Bytes
150962c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
nextLine() - returns String
valueOf() - Integer OR Double



===================================================================


import java.util.Arrays;

public class Example {
    public static void main(String[] args){
        String str = "first,second,third";
        String[] chunks = str.split(",");


        //print array - [first, second, third]
        System.out.println(Arrays.toString(chunks));
        //print string elements
        for (String chunk : chunks) {
            System.out.println(chunk);
        }


        String sentence ="This is a sentence with many words";
        String[] words = sentence.split(" ");
        System.out.println(Arrays.toString(words));
        for (String word : words) {
            System.out.println(word);
        }
    }
}

Program outputs:
[first, second, third]
first
second
third
[This, is, a, sentence, with, many, words]
This
is
a
sentence
with
many
words





===================================================================




eg
luvut/numbers.csv
1,4,3,5,2,4,5,2
4,5,11,2,4,2,5,4,3
6,7,5,2,3,3,2,4,6
8,7,4,5,4,3,3,2,1
0,0,2,4,2,0,2,5,3






reads the file
decomposes each line into chunks, 
converts the chunks to integers and 
finally calculates the sum of the numbers:


import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class Example {
    public static void main(String[] args){
        File file = new File("luvut.csv");
        
        // read the file
        try(Scanner reader = new Scanner(file)) {
            int sum = 0;
            while (reader.hasNextLine()) {

                // process line by line
                String row = reader.nextLine();
                // split into an array of number strings
                String[] chunks = row.split(",");

                // parse each number string as Integer
                for (String chunk : chunks) {
                    int num = Integer.valueOf(chunk);
                    sum += num;
                }
            }
            
            System.out.println("Sum of the numbers: " + sum);
        }  
        catch (FileNotFoundException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}


Program outputs:
Sum of the numbers: 159