import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class WordCounter {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean continueProcessing = true;

        while (continueProcessing) {
            System.out.println("Enter '1' to input text, '2' to read from a file, or '0' to exit:");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline character

            switch (choice) {
                case 1:
                    System.out.println("Enter your text:");
                    String inputText = scanner.nextLine();
                    countAndDisplay(inputText);
                    break;
                case 2:
                    System.out.println("Enter the file path:");
                    String filePath = scanner.nextLine();
                    try {
                        String fileContent = readFile(filePath);
                        countAndDisplay(fileContent);
                    } catch (IOException e) {
                        System.out.println("Error reading file: " + e.getMessage());
                    }
                    break;
                case 0:
                    continueProcessing = false;
                    break;
                default:
                    System.out.println("Invalid choice.");
            }
        }
        scanner.close();
        System.out.println("Program exited.");
    }

    private static void countAndDisplay(String text) {
        int wordCount = text.split("\\s+").length;
        int characterCount = text.length();
        int lineCount = text.split("\n").length;

        System.out.println("Word count: " + wordCount);
        System.out.println("Character count (including spaces): " + characterCount);
        System.out.println("Line count: " + lineCount);
    }

    private static String readFile(String filePath) throws IOException {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                contentBuilder.append(line).append("\n");
            }
        }
        return contentBuilder.toString();
    }
}
