/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/ReadFileFromWeb1.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.lab6.exceptions_calebfontenot;

import java.util.Scanner;

/**
 *
 * @author caleb
 */
public class ReadFileFromWeb1 {
    public static void main(String[] args) {
        System.out.print("Enter a URL (defaults to \"https://calebfontenot.com\"): ");
        String URLinput = new Scanner(System.in).nextLine();
       String URLString;
        if (URLinput.isEmpty()) {
            URLString = "https://calebfontenot.com";
        }
        else {
            URLString = URLinput;
        }
        try {
            java.net.URL url = new java.net.URL(URLString);
            int count = 0, divCount = 0, pCount = 0;
            String text ="";
            Scanner input = new Scanner(url.openStream());
            while (input.hasNext()) {
                String line = input.nextLine();
                count += line.length();
                text += line;
                if (line.contains("<div")) {
                    divCount++;
                }
                if (line.contains("<p")) {
                    pCount++;
                }
                    
            }
            System.out.println("The file size is " + count + " characters.");
            System.out.println("There are " + divCount + " <div> tags on this site.");
            System.out.println("There are " + pCount + " <p> tags on this site.");
            System.out.println(text);
        } catch (java.net.MalformedURLException ex) {
            System.out.println("Invalid URL");
        }
        catch (java.io.IOException ex) {
            System.out.println("IO Errors!");
        }
    }
}