[Gelöst] Sum lines - Java Task

  

4
Themenstarter

Write a program that reads a text file and prints on the console the sum of the ASCII symbols of each of its lines. Use BufferedReader in combination with FileReader.

sum lines in Java

1 Antwort
3

Here is my solution:

import java.io.*;
 
public class Pr_01_SumLines {
    public static void main(String[] args) throws IOException {
        File lines = new File("resources/lines.txt");
        BufferedReader reader = new BufferedReader(new FileReader(lines));
 
        String line = reader.readLine();
 
        while (line != null) {
            int symbolsTotalCount = 0;
            for (int i = 0; i < line.length(); i++) {
                symbolsTotalCount += line.charAt(i);
            }
            System.out.println(symbolsTotalCount);
            line = reader.readLine();
        }
        reader.close();
    }
}
Teilen: