Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StreamsFilesDirectories_Exercise;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.List;
- import java.util.Scanner;
- public class SumLines_01 {
- public static void main(String[] args) throws IOException {
- Scanner scanner = new Scanner(System.in);
- //1. прочетем всички редове от файла
- //2. обходим всеки един ред -> намирам сумата от ascii -> print сумата
- String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
- List<String> allLines = Files.readAllLines(Path.of(path));
- //начин 1:
- /*for (String line : allLines) {
- int sum = 0; //сумата за всеки един ред
- // намирам сумата от ascii -> print сумата
- for (int index = 0; index < line.length(); index++) {
- char currentSymbol = line.charAt(index);
- sum += currentSymbol;
- }
- System.out.println(sum);
- }*/
- //начин 2
- allLines.stream()
- .map(line -> line.toCharArray()) //"Ivan" -> ['I', 'v', 'a', 'n']
- .forEach(arr -> {
- int sum = 0;
- for (char symbol : arr ) {
- sum += symbol;
- }
- System.out.println(sum);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement